PIC16F72單片機(jī)---NOKIA5110測(cè)試程序
/*************PIC16F72單片機(jī)程序******************************/
/*************************************************************/
/*****File Function : NOKIA5110練習(xí)程序 *****/
/*****Program Author : ZhengWen(ClimberWin) *****/
/*****MCU : PIC16F72 外部晶振6MHZ *****/
/*****Compile Date : 2010/11/9 *****/
/*****Edition Info : V1.0 *****/
/*************************************************************/
//備注:下載的時(shí)候需要關(guān)閉掉電檢測(cè),關(guān)閉看們狗,復(fù)位腳需要加電阻到VCC
// PORTB口有弱上拉電阻,可以選用按鍵輸入口,PORTC沒(méi)有上拉,需要外加電阻
/*修改日期: 2010年11月10日 */
//當(dāng)PORTA作為普通I/O使用時(shí)候必須先配置ADCON1,設(shè)定為I/O為數(shù)字I/O
//NOKIA5110測(cè)試OK
/************************************/
#include
#include "english_6x8_pixel.h"
__CONFIG(11111110111001);//bit13-bit7=1;bit6 欠壓使能(1 enable);bit5=1;bit4 代碼保護(hù)(0保護(hù));
//bit3 上電延時(shí)(0 enable);bit2 看門(mén)狗(1 enable);bit1-bit0 時(shí)鐘選擇 (11 RC 10 HS 01 XT OO LP)
#define uchar unsigned char
#define uint unsigned int
void Init(void); //初始化子程序
void LCD_init(void); //LCD初始化程序
void LCD_clear(void);
void LCD_write_english_string(unsigned char X,unsigned char Y,const char *s);
void LCD_write_byte(unsigned char data, unsigned char command);
void delayms(unsigned int count);
void leddisplay(void);
void keytest(void); //按鍵測(cè)試程序
#define LED1 RC0
#define LED2 RC1
#define KEY1 RB0
#define KEY2 RC2
#define KEY3 RC3
#define SPI_CLK RA0
#define SPI_MOSI RA1
#define LCD_DC RA2
#define LCD_CE RA3
#define LCD_RST RA5
/***********************************************/
const unsigned char mask_table[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
/*********************************************/
void delayms(unsigned int count)
{
uint i,j;
for(i=0;i
}
/*********************************************/
void Init(void)
{
PORTA = 0B00000000;
PORTB = 0B00000000;
PORTC = 0B00000000;
TRISA = 0B00000000;//設(shè)置PORTA口為輸出,作為L(zhǎng)CD顯示口
TRISB = 0B00100001;//設(shè)置RB0為輸入,作為按鍵口
TRISC = 0B00001100;//設(shè)置RC2,RC3為輸入,作為按鍵口
RBPU=0;//PORTB上拉使能
ADCON1=0B11111111;//設(shè)置為數(shù)字I/O
LCD_init(); //初始化液晶
}
void LCD_init(void)
{
LCD_RST=0; //LCD復(fù)位
NOP();
LCD_RST=1;
LCD_CE=0 ;// 關(guān)閉LCD
NOP();
LCD_CE=1;// 使能LCD
NOP();
LCD_write_byte(0x21, 0);// 使用擴(kuò)展命令設(shè)置LCD模式
LCD_write_byte(0xc8, 0);// 設(shè)置偏置電壓
LCD_write_byte(0x06, 0);// 溫度校正
LCD_write_byte(0x13, 0);// 1:48
LCD_write_byte(0x20, 0);// 使用基本命令
LCD_clear(); // 清屏
LCD_write_byte(0x0c, 0);// 設(shè)定顯示模式,正常顯示
LCD_CE=0 ; // 關(guān)閉LCD
}
///////////////////////////////////////
/////////LCD清屏程序/////////////
void LCD_clear(void)
{
uint i;
LCD_write_byte(0x0c, 0);
LCD_write_byte(0x80, 0);
for (i=0; i<504; i++)
LCD_write_byte(0x00, 1);//清零
}
///////////設(shè)置LCD坐標(biāo)///////////////////
void LCD_set_XY(unsigned char X, unsigned char Y)
{
LCD_write_byte(0x40 | Y, 0);
LCD_write_byte(0x80 | X, 0);
}
////////////////字符顯示程序/////////////////////
void LCD_write_char(unsigned char c)
{
uint line;
c=c-32;
for (line=0; line<6; line++)
LCD_write_byte( font6x8[c][line], 1);
}
/////////////////打印字符串/////////////////////////
void LCD_write_english_string(unsigned char X,unsigned char Y, const unsigned char *s)
{
uchar i = 0;
LCD_set_XY(X,Y);
while(*s) {LCD_write_char(*s++);}
}
////////////寫(xiě)數(shù)據(jù)到LCD//////////////////////
void LCD_write_byte(unsigned char data, unsigned char command)
{
uchar i;
LCD_CE=0 ; // 使能LCD
if (command == 0)
{LCD_DC=0 ;} // 傳送命令
else
{LCD_DC=1 ;} // 傳送數(shù)據(jù)
for(i=0;i<8;i++)
{
if(data&mask_table[i])
{SPI_MOSI=1;}
else
{SPI_MOSI=0;}
SPI_CLK=0;
NOP();
SPI_CLK=1;
}
LCD_CE=1 ;// 關(guān)閉LCD
}
///////////LED顯示程序///////////////
void leddisplay(void)//LED切換顯示
{
LED1=0;
LED2=1;
delayms(300);
LED1=1;
LED2=0;
delayms(300);
LED1=1;
LED2=1;
}
////////////按鍵測(cè)試程序/////////////////
void keytest(void) //按鍵測(cè)試程序
{
if(KEY1==0)
{
LED1=!LED1;
while(KEY1==0) //按鍵去抖
{ delayms(100);}
}
/////////////////
if(KEY2==0)
{
LED2=!LED2;
while(KEY2==0) //按鍵去抖
{ delayms(100);}
}
////////////////
if(KEY3==0)
{
LED1=!LED1;
LED2=!LED2;
while(KEY3==0) //按鍵去抖
{ delayms(100);}
}
}
////////////主程序/////////////////////////
void main (void)
{
Init();//初始化程序
leddisplay();//LED顯示程序
/* while(1)
{
keytest();
}*/
LCD_clear(); //LCD清屏
delayms(1000);
LCD_write_english_string(0,0,"Nokia5110 LCD" );
LCD_write_english_string(0,1,"MCU:PIC16F72 " );
LCD_write_english_string(0,2,"Version: V1.0" );
LCD_write_english_string(0,3,"Test OK! " );
LCD_write_english_string(0,4,"By ClimberWin" );
LCD_write_english_string(0,5,"2010.11.09 " );
while(1);
}
評(píng)論