Вот код иницилизации портов
CODE
#include "init_gpio.h"
#define SYS_CLK 48000000 // we use SPEED_HIGH = 48 MHz
#define DELAY_TIM_FREQUENCY 1000000 // 1MHZ -> timer runs in microseconds
void init_gpio(void) {
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// Enable or disable the AHB peripheral clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
// Configure GPIO pin (ADC_IN4)
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// Enable or disable the AHB peripheral clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
// Configure GPIO pin
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_1
4;
GPIO_Init(GPIOD, &GPIO_InitStruct);
// Enable timer clock - use TIMER5
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// Time base configuration
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = (SYS_CLK / DELAY_TIM_FREQUENCY) - 1;
TIM_TimeBaseStructure.TIM_Period = UINT16_MAX;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Enable counter */
TIM_Cmd(TIM2, ENABLE);
}
Вот подпрограмма вывода на дисплей
#include "init_gpio.h"
#include "wh2004a.h"
// void Delay_us (uint16_t uSecs) {
// uint16_t Start = (uint16_t)TIM_GetCounter(TIM2);
// while((uint16_t)((uint16_t)TIM_GetCounter(TIM2) - Start) <= uSecs);
// }
void Delay_us (uint16_t uSecs) {
volatile uint32_t Start;
Start = (uint32_t)uSecs*72;
while(Start--);
}
void wh2004_write_4_bit (char Byte, char DI) {
GPIO_ResetBits(GPIOD, GPIO_Pin_14); // E = 0
if (DI) GPIO_SetBits(GPIOD, GPIO_Pin_12); // RS = 1, Write Data
else GPIO_ResetBits(GPIOD, GPIO_Pin_12); // RS = 0, Write Instruction
GPIO_ResetBits(GPIOD, GPIO_Pin_13); // Wn/R = 0
Delay_us(2);
GPIO_SetBits(GPIOD, GPIO_Pin_14); // E = 1
GPIO_SetBits(GPIOD, ((uint16_t)Byte << 8) & 0x0F00);
GPIO_ResetBits(GPIOD, ~((uint16_t)Byte << 8) & 0x0F00);
//GPIO_Write(GPIOB, (GPIOB->ODR & 0xC3FF) | ((((uint16_t)Byte) << 10) & 0x3C00)); // Write Data
Delay_us(2);
GPIO_ResetBits(GPIOD, GPIO_Pin_14); // E = 0
Delay_us(2);
}
void wh2004_write_8_bit (char Byte, char DI) {
wh2004_write_4_bit (Byte >> 4, DI);
wh2004_write_4_bit (Byte, DI);
}
void wh2004_write_data (char Byte, char Pos) {
wh2004_write_8_bit((Pos & 0x7F) | 0x80, 0); // Set Position
Delay_us(50); // Wait 50 us
wh2004_write_8_bit(Byte, 1); // Write Data
Delay_us(50); // Wait 50 us
}
void wh2004_init () {
Delay_us(20000); // Wait 20 ms (Power On)
wh2004_write_4_bit(3, 0); // Set 4-bit mode
Delay_us(5000); // Wait 5 ms
wh2004_write_4_bit(3, 0);
Delay_us(500);
wh2004_write_4_bit(3, 0);
Delay_us(1000);
wh2004_write_4_bit(2, 0);
Delay_us(100);
wh2004_write_8_bit(0x28, 0);
Delay_us(2000); // Wait 100 us
wh2004_write_8_bit(0x0C, 0); // Display Off
Delay_us(2000); // Wait 100 us
wh2004_write_8_bit(0x01, 0); // Display Clear
Delay_us(5000); // Wait 100 us
wh2004_write_8_bit(0x06, 0); // Display Clear
Delay_us(5000); // Wait 100 us
}
Сейчас сделал на задержках функция таймера в верху программы заремлина ибо START при отладке всегда равен 0.