các cao nhân xem giùm code LCD 16x2 bị lỗi gì vậy

hunterhack

Học sinh phổ thông
#1
#include "stm8s.h"
void delay_us(uint16_t x)
{
while(x--)
{
//nop();
//nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
nop();
}
}
void delay_ms(uint16_t x)
{
while(x--)
{
delay_us(1000);
}
}
void port_init()
{
GPIOC->DDR |= 0xff;
GPIOC->CR1 |= 0xff;
GPIOC->CR2 |= 0xff;
}
void LCD_Enable()
{
GPIOC->ODR |= 1<<2;
delay_us(36);
GPIOC->ODR = ~(1<<2);
delay_us(400);
}
void LCD_Send_4bit(unsigned char Data)
{
GPIOC->ODR = (Data & 0x01)<<7;
GPIOC->ODR = ((Data>>1)&1)<<5;
GPIOC->ODR = ((Data>>2)&1)<<3;
GPIOC->ODR = ((Data>>3)&1)<<1;

}
void LCD_SendCommand(unsigned char command)
{
LCD_Send_4bit(command>>4);//gui 4 bit cao
LCD_Enable();
LCD_Send_4bit(command);//gui 4 bit thap
LCD_Enable();
}
void LCD_Clear()
{
LCD_SendCommand(0x01);
delay_us(92);

}
void LCD_Init()
{
LCD_Send_4bit(0x00);
delay_ms(150);
//GPIOC->ODR = (~(1<<4));
GPIOC->ODR = (~(1<<6));
LCD_Send_4bit(0x03);
LCD_Enable();
delay_ms(40);
LCD_Enable();
delay_us(800);
LCD_Enable();
LCD_Send_4bit(0x02);
LCD_Enable();
LCD_SendCommand(0x28);
LCD_SendCommand( 0x0c);
LCD_SendCommand( 0x06 );
LCD_SendCommand(0x01);
}
void LCD_Gotoxy(unsigned char x,unsigned char y)
{
unsigned char address;
if(!y)address = (0x80+x);
else address = (0xc0+x);
delay_ms(8);
LCD_SendCommand(address);
delay_us(400);
}
void LCD_Putchar(unsigned char Data)
{
GPIOC->ODR = 1<<6;
LCD_SendCommand(Data);
GPIOC->ODR = ~(1<<6);

}
void LCD_Puts(char *s)
{
while(*s)
{
LCD_Putchar(*s);
s++;
}
}

void main()
{
CLK->CKDIVR = 0x00;
port_init();
LCD_Init();
LCD_Gotoxy(0,0);
LCD_Puts(" learn STM8 ");
LCD_Gotoxy(0,1);
LCD_Puts("Test LCD 1602 OK");
while(1)
{
}
}
 

huunho

Thạc sỹ
#2
code này không lỗi gì cả
 

Quảng cáo Google