?? 12232.c
字號:
#include <iom32v.h>
#include <macros.h>
#include "1011.h"
#define SS 0//LCD片選(串行) 0:禁止 1:允許
#define MOSI 5//LCD輸入串行數據(串行)
#define SCK 7//LCD輸入串行脈沖(串行)
/*-----------------------------------------------------------------------
延時函數
系統時鐘:8M
-----------------------------------------------------------------------*/
void delay_1us(void) //1us延時函數
{
;
}
void delay_nus(unsigned int n) //N us延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延時函數
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
void port_init(void)
{
DDRB = 0;//設置B口全為輸入
DDRC = 0xff;//設置C口全為輸入
DDRD = 0;//設置D口全為輸入
PORTB = 0xff;//設置B口全部上拉
PORTC = 0xff;//設置C口全部上拉
PORTD = 0xff;//設置D口全部上拉
}
void spi_send(unsigned char data)
{
SPDR = data; // 啟動spi
while(!(SPSR & (1 << SPIF))); // 等待傳輸結束
}
//---------------------------------------------------
// 發送8位LCD控制命令
//--------------------------------------------------
void lcd_send_com(unsigned char com)
{
PORTC |= (1 << SS); //SS=1,啟動SPI
spi_send(0xf8); //發送LCD控制命令
spi_send(com & 0xf0); //發送高4位LCD控制命令
spi_send(com << 4); //發送低4位LCD控制命令
PORTC &= ~(1 << SS); //SS=0,關閉SPI
if (com == 0x01) delay_nms(80); //1.6mS
else delay_nus(200); //st7920要求等待72uS
}
//--------------------------------------------------------
// 發送8位LCD顯示數據
//-------------------------------------------------------
void lcd_send_data(unsigned char data)
{
PORTC |= (1 << SS); //SS=1,啟動SPI
spi_send(0xfa); //發送LCD顯示數據
spi_send(data & 0xf0); //發送高4位LCD顯示數據
spi_send(data << 4); //發送低4位LCD顯示數據
PORTC &= ~(1 << SS); //SS=0,關閉SPI
delay_nus(200); //st7920要求等待72uS
}
//---------------------------------------------------
// LCD初始化設置
//---------------------------------------------------
void lcd_init(void)
{
delay_nms(10); //上電等待延時1000Ms
spi_init(); //SPI初始化
lcd_send_com(0b00100000); //發送4位控制命令
lcd_send_com(0b00000100); //發送進入點命令
lcd_send_com(0b00001100); //發送開顯示關光標命令
lcd_send_com(0b00000001); //發送清除顯示命令
lcd_send_com(0b10000000); //發送設定DDRAM地址0x00命令
}
//------------------------------------------
//定位
//----------------------------------------
unsigned char set_lcd_position(unsigned char row, unsigned char col)
{
if ((row < 2) && (col < 8)) //漢字字符為2行7.5列(漢字必須偶數對齊)
{
lcd_send_com(0x80 + row * 16 + col);//發送設定DDRAM地址row * 16 + col命令
return 1;//成功返回
}
else
return 0;//失敗返回
}
//---------------------------------------------------
//顯示字符串
//---------------------------------------------------
void lcd_display_string(unsigned char * string)
{
while(*string) lcd_send_data(*string ++);
}
//------------------------------------------------------------------------------------
//在指定位置顯示
//------------------------------------------------------------------------------------
void lcd_display(unsigned char row, unsigned char col, unsigned char * string)
{
if (set_lcd_position(row, col))
{
lcd_display_string(string);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -