When i apply a reset via NRST pin my program does not work properly. If i pull down to ground all pins my program starts.
/********************************** (C) COPYRIGHT *******************************
- File Name : main.c
- Author : WCH
- Version : V1.0.0
- Date : 2021/06/06
- Description : Main program body.
- Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
- Attention: This software (modified or not) and binary are used for
- microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/*
*@Note
USART Print debugging routine:
USART1_Tx(PA9).
This example demonstrates using USART1(PA9) as a print debug port output.
*/
#include “debug.h”
#define LCD_RS GPIOB,GPIO_Pin_13
#define LCD_EN GPIOB,GPIO_Pin_12
#define LCD_DB4 GPIOB,GPIO_Pin_14
#define LCD_DB5 GPIOB,GPIO_Pin_15
#define LCD_DB6 GPIOA,GPIO_Pin_8
#define LCD_DB7 GPIOA,GPIO_Pin_9
#define clear_display 0x01
#define goto_home 0x02
#define cursor_direction_inc (0x04 | 0x02)
#define cursor_direction_dec (0x04 | 0x00)
#define display_shift (0x04 | 0x01)
#define display_no_shift (0x04 | 0x00)
#define display_on (0x08 | 0x04)
#define display_off (0x08 | 0x02)
#define cursor_on (0x08 | 0x02)
#define cursor_off (0x08 | 0x00)
#define blink_on (0x08 | 0x01)
#define blink_off (0x08 | 0x00)
#define _8_pin_interface (0x20 | 0x10)
#define _4_pin_interface (0x20 | 0x00)
#define _2_row_display (0x20 | 0x08)
#define _1_row_display (0x20 | 0x00)
#define _5x10_dots (0x20 | 0x40)
#define _5x7_dots (0x20 | 0x00)
#define DAT 1
#define CMD 0
void LCD_init(void)
{
// LCD_GPIO_init();
toggle_EN_pin();
GPIO_ResetBits(LCD_RS);
GPIO_ResetBits(LCD_DB7);
GPIO_ResetBits(LCD_DB6);
GPIO_SetBits(LCD_DB5);
GPIO_SetBits(LCD_DB4);
toggle_EN_pin();
GPIO_ResetBits(LCD_DB7);
GPIO_ResetBits(LCD_DB6);
GPIO_SetBits(LCD_DB5);
GPIO_SetBits(LCD_DB4);
toggle_EN_pin();
GPIO_ResetBits(LCD_DB7);
GPIO_ResetBits(LCD_DB6);
GPIO_SetBits(LCD_DB5);
GPIO_SetBits(LCD_DB4);
toggle_EN_pin();
GPIO_ResetBits(LCD_DB7);
GPIO_ResetBits(LCD_DB6);
GPIO_SetBits(LCD_DB5);
GPIO_ResetBits(LCD_DB4);
toggle_EN_pin();
LCD_send((_4_pin_interface | _2_row_display | _5x7_dots), CMD);
LCD_send((display_on | cursor_off | blink_off), CMD);
LCD_send(clear_display, CMD);
LCD_send((cursor_direction_inc | display_no_shift), CMD);
}
void LCD_send(unsigned char value, unsigned char mode)
{
switch(mode)
{
case DAT:
{
GPIO_SetBits(LCD_RS);
break;
}
case CMD:
{
GPIO_ResetBits(LCD_RS);
break;
}
}
LCD_4bit_send(value);
}
void LCD_4bit_send(unsigned char lcd_data)
{
toggle_io(lcd_data, 7, 7);
toggle_io(lcd_data, 6, 6);
toggle_io(lcd_data, 5, 5);
toggle_io(lcd_data, 4, 4);
toggle_EN_pin();
toggle_io(lcd_data, 3, 7);
toggle_io(lcd_data, 2, 6);
toggle_io(lcd_data, 1, 5);
toggle_io(lcd_data, 0, 4);
toggle_EN_pin();
}
void LCD_putstr(char *lcd_string)
{
do
{
LCD_send(*lcd_string++, DAT);
}while(*lcd_string != ‘\0’);
}
void LCD_putchar(char char_data)
{
LCD_send(char_data, DAT);
}
void LCD_clear_home(void)
{
LCD_send(clear_display, CMD);
LCD_send(goto_home, CMD);
}
void LCD_goto(unsigned char x_pos, unsigned char y_pos)
{
if(y_pos == 0)
{
LCD_send((0x80 | x_pos), CMD);
}
if(y_pos == 1)
{
LCD_send((0xC0+x_pos), CMD);
}
if(y_pos == 2)
{
LCD_send((0x94+x_pos), CMD);
}
if(y_pos == 3)
{
LCD_send((0xD4+x_pos), CMD);
}
}
void toggle_EN_pin(void)
{
GPIO_SetBits(LCD_EN);
Delay_Ms(2);
GPIO_ResetBits(LCD_EN);
}
void toggle_io(unsigned char lcd_data, unsigned char bit_pos, unsigned char pin_num)
{
u8 temp1 = RESET;//FALSE
temp1 = (0x01 & (lcd_data >> bit_pos));
switch(temp1)
{
case SET://TRUE
{
if(pin_num==7)
GPIO_SetBits(LCD_DB7);
if(pin_num==6)
GPIO_SetBits(LCD_DB6);
if(pin_num==5)
GPIO_SetBits(LCD_DB5);
if(pin_num==4)
GPIO_SetBits(LCD_DB4);
break;
}
default:
{
if(pin_num==7)
GPIO_ResetBits(LCD_DB7);
if(pin_num==6)
GPIO_ResetBits(LCD_DB6);
if(pin_num==5)
GPIO_ResetBits(LCD_DB5);
if(pin_num==4)
GPIO_ResetBits(LCD_DB4);
break;
}
}
}
void GPIO_Toggle_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIO_Init(GPIOD,GPIO_Pin_1,GPIO_Speed_50MHz,GPIO_Mode_Out_PP);
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
SystemCoreClockUpdate();
Delay_Init();
//USART_Printf_Init(115200);
// printf(“SystemClk:%d\r\n”,SystemCoreClock);
// printf( “ChipID:%08x\r\n”, DBGMCU_GetCHIPID() );
// printf(“This is printf example\r\n”);
//GPIO_AFIODeInit();
//GPIO_DeInit(GPIOA);
//GPIO_DeInit(GPIOB);
//ADC_DeInit(ADC1);
//ADC_DeInit(ADC2);
GPIO_Toggle_INIT();
//Delay_Ms(1000);
GPIO_SetBits(GPIOA,GPIO_Pin_7);
Delay_Ms(1000);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
Delay_Ms(1000);
LCD_init();
while(1)
{
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_11)==Bit_RESET)
{
LCD_init();
LCD_goto(0,0);
LCD_putstr(“Buton1”);
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_11)==Bit_RESET)
{}
}//buton1
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12)==Bit_RESET)
{
LCD_init();
LCD_goto(0,0);
LCD_putstr(“Buton2”);
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12)==Bit_RESET)
{}
}//buton2
if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)==Bit_RESET)
{
LCD_init();
LCD_goto(0,0);
LCD_putstr(“Buton3”);
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_15)==Bit_RESET)
{}
}//buton3
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3)==Bit_RESET)
{
LCD_init();
LCD_goto(0,0);
LCD_putstr(“Buton4”);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_3)==Bit_RESET)
{}
}//buton4
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4)==Bit_RESET)
{
LCD_init();
LCD_goto(0,0);
LCD_putstr(“Buton5”);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_4)==Bit_RESET)
{}
}//buton5
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET)
{
LCD_init();
LCD_goto(0,0);
LCD_putstr(“Buton6”);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_5)==Bit_RESET)
{}
}//buton6
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6)==Bit_RESET)
{
LCD_init();
LCD_goto(0,0);
LCD_putstr(“Buton7”);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_6)==Bit_RESET)
{}
}//buton7
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)==Bit_RESET)
{
LCD_init();
LCD_goto(0,0);
LCD_putstr(“Buton8”);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)==Bit_RESET)
{}
}//buton8
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)==Bit_RESET)
{
LCD_init();
LCD_goto(0,0);
LCD_putstr(“Buton9”);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_8)==Bit_RESET)
{}
}//buton9
}
}