|
第一次學(xué)Arduino......
還沒有購買開發(fā)板
一般都在Proteus上進(jìn)行
51單片機(jī):http://www.zg4o1577.cn/bbs/dpj-114268-1.html
360截圖17290508183222.png (102.33 KB, 下載次數(shù): 97)
下載附件
整體電路圖
2018-4-30 11:48 上傳
360截圖17290505265132.png (27.45 KB, 下載次數(shù): 83)
下載附件
效果圖
2018-4-30 11:48 上傳
360截圖17670915557159.png (28.29 KB, 下載次數(shù): 71)
下載附件
效果圖
2018-4-30 11:48 上傳
單片機(jī)代碼如下:
- //引腳定義
- #define LCD1602_SHCP 0
- #define LCD1602_DS 1
- #define LCD1602_STCP 2
- //兩種數(shù)據(jù)類型
- typedef unsigned char uchar;
- typedef unsigned int uint;
- //定義數(shù)據(jù)槽
- uchar RS,RW,E,D4,D5,D6,D7;
- //二進(jìn)制的形象表示方法,從左到右分別位第7 6 5 4 3 2 1 0位
- #define bin(_a,_b,_c,_d,_e,_f,_g,_h) ((_a)<<7|(_b)<<6|(_c)<<5|(_d)<<4|(_e)<<3|(_f)<<2|(_g)<<1|(_h)) //二進(jìn)制的表示
- void LCD1602_DATAPINS(uchar value); //更改74HC595連接LCD1602的數(shù)據(jù)
- void LcdCalc(uchar i); //確定4位總線
- void LcdWriteCom(uchar com); //向LCD寫入一個字節(jié)的命令
- void LcdWriteData(uchar dat); //向LCD寫入一個字節(jié)的數(shù)據(jù)
- void LcdInit(); //初始化LCD屏
- void LcdPrintf(uchar *s); //向屏幕輸出字符
- void LcdWriteCGRAM(uchar index,uchar *p); //建立一個自定義字符
- void LCDPrintNum(uint num,uchar wei); //顯示數(shù)字
- /*******************************************************************************
- * 函 數(shù) 名 : setup
- * 函數(shù)功能 : 準(zhǔn)備
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void setup()
- {
- pinMode(LCD1602_SHCP,OUTPUT);
- pinMode(LCD1602_DS,OUTPUT);
- pinMode(LCD1602_STCP,OUTPUT);
- }
- /*******************************************************************************
- * 函 數(shù) 名 : LCD1602_DATAPINS
- * 函數(shù)功能 : 更改74HC595連接LCD1602的數(shù)據(jù)
- * 輸 入 : value
- * 輸 出 : 無
- *******************************************************************************/
- void LCD1602_DATAPINS(uchar value)
- {
- uchar i,j;
- for(i=0;i<8;i++)
- {
- j=value&0x80;//取數(shù)據(jù)高位
- if(j==0x80){ //判斷數(shù)據(jù)高位是否為1
- digitalWrite(LCD1602_DS,HIGH);//如果高位為1,DS置1
- }
- else{
- digitalWrite(LCD1602_DS,LOW);//否則DS置0
- }
- digitalWrite(LCD1602_SHCP,LOW);
- digitalWrite(LCD1602_SHCP,HIGH);//上升沿使這一位寫入移位寄存器
- value<<=1;//數(shù)據(jù)左移1位,數(shù)據(jù)的第7位變?yōu)樽罡呶唬h(huán)第8次時數(shù)據(jù)的最低位也變成最高位
- }
- digitalWrite(LCD1602_STCP,LOW);
- digitalWrite(LCD1602_STCP,HIGH);//給STCP一個上升沿,將for循環(huán)中輸入的數(shù)據(jù)移入輸出鎖存寄存器,刷新輸出數(shù)據(jù)
- }
- /*******************************************************************************
- * 函 數(shù) 名 : LcdCalc
- * 函數(shù)功能 : 確定4位總線
- * 輸 入 : i
- * 輸 出 : 無
- *******************************************************************************/
- void LcdCalc(uchar i)
- {
- if(i==0x00){
- D4=0;
- D5=0;
- D6=0;
- D7=0;
- }
- if(i==0x01){
- D4=1;
- D5=0;
- D6=0;
- D7=0;
- }
- if(i==0x02){
- D4=0;
- D5=1;
- D6=0;
- D7=0;
- }
- if(i==0x03){
- D4=1;
- D5=1;
- D6=0;
- D7=0;
- }
- if(i==0x04){
- D4=0;
- D5=0;
- D6=1;
- D7=0;
- }
- if(i==0x05){
- D4=1;
- D5=0;
- D6=1;
- D7=0;
- }
- if(i==0x06){
- D4=0;
- D5=1;
- D6=1;
- D7=0;
- }
- if(i==0x07){
- D4=1;
- D5=1;
- D6=1;
- D7=0;
- }
- if(i==0x08){
- D4=0;
- D5=0;
- D6=0;
- D7=1;
- }
- if(i==0x09){
- D4=1;
- D5=0;
- D6=0;
- D7=1;
- }
- if(i==0x0a){
- D4=0;
- D5=1;
- D6=0;
- D7=1;
- }
- if(i==0x0b){
- D4=1;
- D5=1;
- D6=0;
- D7=1;
- }
- if(i==0x0c){
- D4=0;
- D5=0;
- D6=1;
- D7=1;
- }
- if(i==0x0d){
- D4=1;
- D5=0;
- D6=1;
- D7=1;
- }
- if(i==0x0e){
- D4=0;
- D5=1;
- D6=1;
- D7=1;
- }
- if(i==0x0f){
- D4=1;
- D5=1;
- D6=1;
- D7=1;
- }
- if(i==0x10){
- D4=1;
- D5=0;
- D6=0;
- D7=0;
- }
- if(i==0x20){
- D4=0;
- D5=1;
- D6=0;
- D7=0;
- }
- if(i==0x30){
- D4=1;
- D5=1;
- D6=0;
- D7=0;
- }
- if(i==0x40){
- D4=0;
- D5=0;
- D6=1;
- D7=0;
- }
- if(i==0x50){
- D4=1;
- D5=0;
- D6=1;
- D7=0;
- }
- if(i==0x60){
- D4=0;
- D5=1;
- D6=1;
- D7=0;
- }
- if(i==0x70){
- D4=1;
- D5=1;
- D6=1;
- D7=0;
- }
- if(i==0x80){
- D4=0;
- D5=0;
- D6=0;
- D7=1;
- }
- if(i==0x90){
- D4=1;
- D5=0;
- D6=0;
- D7=1;
- }
- if(i==0xa0){
- D4=0;
- D5=1;
- D6=0;
- D7=1;
- }
- if(i==0xb0){
- D4=1;
- D5=1;
- D6=0;
- D7=1;
- }
- if(i==0xc0){
- D4=0;
- D5=0;
- D6=1;
- D7=1;
- }
- if(i==0xd0){
- D4=1;
- D5=0;
- D6=1;
- D7=1;
- }
- if(i==0xe0){
- D4=0;
- D5=1;
- D6=1;
- D7=1;
- }
- if(i==0xf0){
- D4=1;
- D5=1;
- D6=1;
- D7=1;
- }
- }
- /*******************************************************************************
- * 函 數(shù) 名 : LcdWriteCom
- * 函數(shù)功能 : 向LCD寫入一個字節(jié)的命令
- * 輸 入 : com
- * 輸 出 : 無
- *******************************************************************************/
- void LcdWriteCom(uchar com) //寫入命令
- {
- E = 0; //使能清零
- RS = 0; //選擇寫入命令
- RW = 0; //選擇寫入
- LcdCalc(com>>4); //發(fā)送高四位
- E = 1; //寫入時序
- LCD1602_DATAPINS(bin(0,D7,D6,D5,D4,E,RW,RS));
- E = 0;
- LCD1602_DATAPINS(bin(0,D7,D6,D5,D4,E,RW,RS));
- LcdCalc(com<<4); //發(fā)送低四位
- E = 1; //寫入時序
- LCD1602_DATAPINS(bin(0,D7,D6,D5,D4,E,RW,RS));
- E = 0;
- LCD1602_DATAPINS(bin(0,D7,D6,D5,D4,E,RW,RS));
- }
- /****************************************************************************
- * 函 數(shù) 名 : LcdWriteData
- * 函數(shù)功能 : 向LCD寫入一個字節(jié)的數(shù)據(jù)
- * 輸 入 : dat
- * 輸 出 : 無
- *******************************************************************************/
- void LcdWriteData(uchar dat) //寫入數(shù)據(jù)
- {
- E = 0; //使能清零
- RS = 1; //選擇寫入數(shù)據(jù)
- RW = 0; //選擇寫入
- LcdCalc(dat>>4); //發(fā)送高四位
- E = 1; //寫入時序
- LCD1602_DATAPINS(bin(0,D7,D6,D5,D4,E,RW,RS));
- E = 0;
- LCD1602_DATAPINS(bin(0,D7,D6,D5,D4,E,RW,RS));
- LcdCalc(dat<<4); //發(fā)送低四位
- E = 1; //寫入時序
- LCD1602_DATAPINS(bin(0,D7,D6,D5,D4,E,RW,RS));
- E = 0;
- LCD1602_DATAPINS(bin(0,D7,D6,D5,D4,E,RW,RS));
- }
- /*******************************************************************************
- * 函 數(shù) 名 : LcdInit
- * 函數(shù)功能 : 初始化LCD屏
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void LcdInit() //LCD初始化子程序
- {
- LcdWriteCom(0x32); //將8位總線轉(zhuǎn)為4位總線
- LcdWriteCom(0x28); //在四位線下的初始化
- LcdWriteCom(0x0c); //開顯示不顯示光標(biāo)
- LcdWriteCom(0x06); //寫一個指針加1
- LcdWriteCom(0x01); //清屏
- LcdWriteCom(0x80); //設(shè)置數(shù)據(jù)指針起點(diǎn)
- }
- /*******************************************************************************
- * 函 數(shù) 名 : LcdPrintf
- * 函數(shù)功能 : 向屏幕輸出字符
- * 輸 入 : *s
- * 輸 出 : 無
- *******************************************************************************/
- void LcdPrintf(uchar *s)
- {
- while(*s>0)LcdWriteData(*s++);
- }
- /*******************************************************************************
- * 函 數(shù) 名 : LcdWriteCGRAM
- * 函數(shù)功能 : 建立一個自定義字符
- * 輸 入 : index、*p
- * 輸 出 : 無
- *******************************************************************************/
- void LcdWriteCGRAM(uchar index,uchar *p)
- {
- uchar i;
- index <<=3; //index *= 8
- for(i=0;i<8;++i){
- LcdWriteCom(0x40 | index+i);//寫CGRAM地址
- LcdWriteData(*p++);
- }
- LcdWriteCom(0x80);
- }
- /*******************************************************************************
- * 函 數(shù) 名 : LcdPrintNum
- * 函數(shù)功能 : 顯示數(shù)字
- * 輸 入 : num、wei
- * 輸 出 : 無
- *******************************************************************************/
- void LCDPrintNum(uint num,uchar wei)//wei 數(shù)字占的位數(shù)
- {
- uchar tempstring[6];
- uchar n[5];
- uchar i,j=0;
- n[0]=num/10000;
- n[1]=num%10000/1000;
- n[2]=num%1000/100;
- n[3]=num%100/10;
- n[4]=num%10;
- i=5-wei;
- for(i;i<5;i++){
- tempstring[j++]=n[i]+'0';
- }
- tempstring[j]='\0';
- LcdPrintf(tempstring);
- }
- const uchar type_t[]={
- 0x1f,0x02,0x02,0x02,
- 0x02,0x02,0x02,0x00
- };
- const uchar line1[]="Have a nice day!";
- const uchar line2[]="HE Wcommands";
- void loop()
- {
- uchar i;
- LcdInit();
- LcdWriteCGRAM(0x00,type_t);
- LcdWriteData(0x00);
- LcdWriteCom(0x81);
- LcdPrintf("HE With");
- LcdWriteCom(0xc0);
- LcdPrintf("Commands");
- delay(100);
- LcdWriteCom(0x01);
- while(1){
- for(i=0;i<16;i++){
- LcdWriteCom(0x80+i);
- LcdWriteData(line1[i]);
- delay(5);
- }
- delay(10);
- LcdWriteCom(0xc1);
- LcdWriteData(0x00);
- delay(5);
- for(i=0;i<12;i++){
- LcdWriteCom(0xc2+i);
- LcdWriteData(line2[i]);
- delay(5);
- }
- LcdWriteCom(0x01);
- delay(5);
- }
- }
復(fù)制代碼
0.png (48.4 KB, 下載次數(shù): 94)
下載附件
2018-4-30 15:05 上傳
全部資料51hei下載地址:
lcd595.zip
(27.79 KB, 下載次數(shù): 31)
2018-4-30 11:55 上傳
點(diǎn)擊文件名下載附件
程序文件及仿真 下載積分: 黑幣 -5
|
評分
-
查看全部評分
|