前幾年做的操控狀態儀表,里邊有COG液晶,串口,按鍵,IO,溫濕度傳感器,EEPROM等等
對于單片機的初學者有幫助,可以了解單片機和外設工作的基本原理
和簡單的數據處理,
0.jpg (35.78 KB, 下載次數: 125)
下載附件
2018-5-5 15:28 上傳
全部資料51hei下載地址:
CaoKonog20120110.rar
(850.75 KB, 下載次數: 28)
2018-5-5 12:47 上傳
點擊文件名下載附件
操控狀態 下載積分: 黑幣 -5
單片機源碼如下:
- #include <iom64v.h>
- #include <macros.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <eeprom.h>
- #include <math.h>
- /*********************************************************************************************************/
- #include "IO.h"
- #include "uart.h"
- #include "lcd.h"
- #include "key.h"
- #include "sht.h"
- /*********************************************************************************************************/
- #pragma data:data
-
- unsigned char temp_flag=0;
- unsigned char wd_shang;
- unsigned char wd_xia;
- unsigned char sd_shang;
- unsigned char sd_xia;
- unsigned char pf_shang;
- unsigned char pf_xia;
- unsigned char Body_Flag; //人體靠近帶電標志位
- unsigned char rt_gy; // 人體感應非投入,1:投入,2:非投入
- unsigned char db_du;
- unsigned char InIt_Para[]={0x0a,0x05,0x5D,0x55,0x37,0x32,0x01,0x01};
- /*********************************************************************************************************/
- unsigned char wd_bz;
- unsigned char sd_bz;
- unsigned char pf_bz;
- unsigned char wendu1_biaozhi;
- unsigned char wendu2_biaozhi;
- unsigned char mima;
- /*********************************************************************************************************/
- unsigned int R_ms_Timer,mincount1;
- unsigned int timer_count;
- unsigned char timer_count1;
- unsigned char timer_count2;
- unsigned char timer_count3;
- unsigned char Cls_Lcd_Flag;
- unsigned char GPIOSamp_counter_Flag;
- unsigned char GPIOSamp_counter;
- /*********************************************************************************************************/
- extern unsigned char bz; //位標志
- extern unsigned char bz1; //位標志,判斷是地址還是數據
- extern unsigned char di_zhi;
- extern unsigned char Receiver_Flag; // 接受數據標志
- extern unsigned char Back_Light_Flag;
- extern unsigned char rx0_buf[16]; // 接收數據緩存器
- extern unsigned char rx0_index; // 接收數據計數器
- extern unsigned char Tx0_buf[16]; // 發送數據緩存器
- extern unsigned char R_KeyValue1;
- /*********************************************************************************************************/
- //todao[0]:濕度測量 1; todao[1]: 溫度測量 1; todao[2]:濕度測量 2; todao[3]:溫度測量 2
- extern float todao[4]; //{temp,rh}; 模擬量寄存器從1到2
-
- extern unsigned char rx0_index;
-
- extern unsigned char Q,n,TT1,T,TT2;
- extern unsigned char ch,cl;
- extern unsigned char Up_Counter;
- extern unsigned char Page_Flag;
- extern unsigned char Page_Flag1;
- /**********************************************************************************************************
- ** 函數名稱:init_para()
- ** 函數功能:開機時取得、設定各項參數
- ** 入口參數:
- ** 出口參數:無
- ** 說 明:
- *********************************************************************************************************/
- void init_para(void)
- {
- EEPROMReadBytes(0x0010,&wd_shang,1);
- delay_nms(10);
- EEPROMReadBytes(0x0011,&wd_xia,1);
-
- delay_nms(10);
- EEPROMReadBytes(0x0012,&sd_shang,1);
- EEPROMReadBytes(0x0013,&sd_xia,1);
- delay_nms(10);
- EEPROMReadBytes(0x0014,&pf_shang,1);
- EEPROMReadBytes(0x0015,&pf_xia,1);
- EEPROMReadBytes(0x0016,&rt_gy,1);
- EEPROMReadBytes(0x0017,&di_zhi,1);
-
- }
- /**********************************************************************************************************
- * 函數名稱:EEprom_init()
- * 函數功能:開機時取得、設定各項參數
- * 入口參數:
- * 出口參數:無
- * 說 明:
- ********************************************************************************************************/
- void EEprom_init(void)
- {
- unsigned char EEP_Read=0,Eep_Write=0;
-
- EEPROMReadBytes(0x03,&EEP_Read,1);
- if( EEP_Read != 0x55)
- {
- Eep_Write = 0x55;
- EEPROMWriteBytes(0x0003,&Eep_Write,1);
- EEPROMWriteBytes(0x0010,InIt_Para,8);
- init_para();
- }
- else
- {
- init_para();
- }
- }
- /**********************************************************************************************************
- ** 函數名稱:timer0_init()
- ** 函數功能:初始化定時器0,10ms定時器
- ** 入口參數:無
- ** 出口參數:無
- ** 說明: 8位定時器,和8051相似,只是注意定時器時鐘是晶振分頻以后
- *********************************************************************************************************/
- void Timer0_init(void)
- {
- TCCR0 = 0x00;
- ASSR = 0x00;
- TCNT0 = 0xB1; // 定時初值 8Mhz
- //TCNT0 = 0x93; // 定時初值 11Mhz
- OCR0 = 0x00;
- TCCR0 = 0x07; // 時鐘1024分頻 ,啟動定時器
- TIMSK |= 0x01; // timer0中斷使能
- }
- /**********************************************************************************************************
- ** 函數名稱:timer0_ovf_isr()
- ** 函數功能:定時器0溢出中斷服務程序
- ** 入口參數:無
- ** 出口參數:無
- ** 說明:
- *********************************************************************************************************/
- #pragma interrupt_handler Timer0_ovf_isr:iv_TIMER0_OVF
- void Timer0_ovf_isr(void)
- {
- TCNT0 = 0xB1;
- R_ms_Timer++;
- timer_count1++;
- if(timer_count1>=4)
- {
- KeyValue_Pro();
- timer_count1=0;
- }
- if(GPIOSamp_counter_Flag)
- GPIOSamp_counter++;
-
-
- if((Back_Light_Flag==1)&&((Body_Flag == 0)||(Body_Flag == 2))) // 液晶背光
- {
- mincount1++;
- if(mincount1 >= Ldc_Timer ) // 30秒
- {
- mincount1 = 0;
- DI_jdq5_ZHMIN_OUT1;
- LCD_LED_OUT1;
- }
- }
- if(Back_Light_Flag==0)
- {
- LCD_LED_OUT0;
- DI_jdq5_ZHMIN_OUT0;
- mincount1 = 0;
- }
-
- }
- /**********************************************************************************************************
- ** 函數名稱:timer1_init()
- ** 函數功能:初始化定時器1
- ** 入口參數:無
- ** 出口參數:無
- ** 說明:
- ** 16位定時器,和8051相似,只是注意定時器時鐘是晶振分頻以后
- ** TCNT1 = 65536 - (Focs X m) / 1024; m: 為定時時間,單位 s
- ** TCNT1分為TCNT1H和TCNT1L
- *********************************************************************************************************/
- void Timer1_init(void)
- {
- TCCR1B = 0x00; //stop
-
- //TCNT1H = 0x0D5; //計數初值 , 定時為1秒 Focs = 11.0592Mhz
- //TCNT1L = 0x0D0;
-
- //TCNT1H = 0x02D; //計數初值 , 定時為5秒 Focs = 11.0592Mhz
- //TCNT1L = 0x00F;
-
- //TCNT1H = 0x0E1; //計數初值, 定時為1秒 Focs = 8Mhz
- //TCNT1L = 0x07B;
- TCNT1H = 0x0F0; //計數初值, 定時為0.5秒 Focs = 8Mhz
- TCNT1L = 0x0BD;
-
- OCR1AH = 0x98; // 輸出比較寄存器A初值
- OCR1AL = 0x96;
-
- OCR1BH = 0x98; // 輸出比較寄存器B初值
- OCR1BL = 0x96;
-
- ICR1H = 0x98; // 輸入捕獲寄存器初值
- ICR1L = 0x96;
-
- TCCR1A = 0x00; // 比較匹配模式禁止
- TCCR1B = 0x05; // start Timer,時鐘分頻1024分頻
- TIMSK |= 0x04; // timer1中斷使能
- }
- /**********************************************************************************************************
- ** 函數名稱:timer1_ovf_isr()
- ** 函數功能:定時器1溢出中斷服務程序
- ** 入口參數:
- ** 出口參數:無
- ** 說 明:500ms
- *********************************************************************************************************/
- #pragma interrupt_handler Timer1_ovf_isr:iv_TIMER1_OVF
- void Timer1_ovf_isr(void)
- {
- TCNT1H = 0x0F0;
- TCNT1L = 0x0BD;
- timer_count2++;
- if((Page_Flag !=0))
- {
- timer_count3++;
- if(timer_count3 >=60) // 一定時間內沒有按鍵,強制返回溫濕度顯示頁面
- {
- timer_count3 = 0;
- Cls_Lcd_Flag = 1;
- }
- }
- if(timer_count2 >= 2) // 1.5秒重新檢測一次溫濕度
- {
- timer_count2 = 0;
- temp_flag=1;
- }
- if(Body_Flag == 1) // 人體感應設置為投時,檢測到人體感應時
- {
- LCD_LED_OUT0;
- DI_jdq5_ZHMIN_OUT0;
- timer_count = 0;
- }
- if(Body_Flag == 0) // 人體感應設置為投時,未檢測到人體感應時
- {
- timer_count++;
- if(timer_count >= Body_Timer ) // 1分鐘后關閉液晶背光
- {
- timer_count = 0;
- LCD_LED_OUT1;
- DI_jdq5_ZHMIN_OUT1;
- }
- }
- }
- /**********************************************************************************************************
- ** 函數名稱:QuanJu_Var_Init()
- ** 函數功能:全局變量初始化
- ** 入口參數:
- ** 出口參數:無
- ** 說 明:
- *********************************************************************************************************/
- void QuanJu_Var_Init(void)
- {
-
- bz1 = 1;
- T=15;
- TT1=18;
- TT2=18;
- Q=n=ch=cl=0;
- rx0_index = 0;
- Up_Counter = 1;
- Receiver_Flag = 0;
- Back_Light_Flag = 1;
- db_du =0x70;
- Body_Flag = 0;
- LCD_LED_OUT0;
- memset(Tx0_buf,0,sizeof(Tx0_buf));
- memset(rx0_buf,0,sizeof(rx0_buf));
- memset(todao,0,sizeof(todao));
- }
- /**********************************************************************************************************
- ** 函數名稱:Hardware_Init()
- ** 函數功能:所有外設端口初始化
- ** 入口參數:
- ** 出口參數:無
- ** 說 明:
- *********************************************************************************************************/
- void Hardware_Init(void)
- {
- //keyboard
- PORTD |= 0x0f0; //輸入置為上拉
-
- KY1_IN;
- KY2_IN;
- KY3_IN;
- KY4_IN;
- // 溫濕度
- SHT10_SCK1_OUT;
- SHT10_SDA1_OUT;
- SHT10_SCK2_OUT;
- SHT10_SDA2_OUT;
- //LED燈
- DI_led1_OUT;
- DI_led2_OUT;
- DI_led3_OUT;
- DI_led4_OUT;
- DI_led5_OUT;
- DI_led6_OUT;
- DI_led7_OUT;
- DI_led8_OUT;
-
- DI_led1_OUT0;
- DI_led2_OUT1;
- DI_led3_OUT0;
- DI_led4_OUT0;
- DI_led5_OUT0;
- DI_led6_OUT0;
- DI_led7_OUT0;
- DI_led8_OUT1;
-
- //繼電器
- DI_jdq0_JRA_OUT;
- DI_jdq1_JRB_OUT;
- DI_jdq2_PF_OUT;
- DI_jdq3_BJ_OUT;
- DI_jdq4_BS_OUT;
- DI_jdq5_ZHMIN_OUT;
-
- DI_jdq0_JRA_OUT1;
- DI_jdq1_JRB_OUT1;
- DI_jdq2_PF_OUT1;
- DI_jdq3_BJ_OUT1;
- DI_jdq4_BS_OUT1;
- DI_jdq5_ZHMIN_OUT1;
-
- //語音
- DI_yu_ying1_OUT;
- DI_yu_ying2_OUT;
- DI_yu_ying3_OUT;
- DI_yu_ying4_OUT;
- DI_yu_ying5_ZD_OUT;
- DI_yu_ying1_OUT0;
- DI_yu_ying2_OUT0;
- DI_yu_ying3_OUT0;
- DI_yu_ying4_OUT0;
- DI_yu_ying5_ZD_OUT0;
- //HC165輸入端口
- HC165_SCK_OUT;
- HC165_LD_OUT;
- HC165_SD_IN;
- PORTG |= 0x04;
- // 人體感應
- Re_gyin_RT;
- PORTD |= 0x01;
- //LCD端口
- LCD_DIR1_OUT;
- LCD_DIR2_OUT;
-
- LCD_CS_OUT;
- LCD_RST_OUT;
- LCD_RS_OUT;
- LCD_RW_OUT;
- LCD_ERW_OUT;
- LCD_LED_OUT;
-
- DDRA = 0xff; // PA為輸出
-
- LCD_DIR1_OUT1;
- LCD_DIR2_OUT1;
- //RS485 接收發送選擇:接收
- SET_485_EN_OUT;
- SET_485_EN_OUT1;
-
- DI_jdq5_ZHMIN_OUT0; //照明
-
- }
- /**********************************************************************************************************
- ** 函數名稱:main()
- ** 函數功能:
- ** 入口參數:
- ** 出口參數:無
- ** 說 明:
- *********************************************************************************************************/
- void main(void)
- {
- CLI();
- EEprom_init();
- Hardware_Init();
- uart0_init();
- Timer0_init();
- Timer1_init();
- QuanJu_Var_Init();
- Lcd_Init();
- Temp_Humi_Init();
- SEI();
- Init_LcdDis();
- while(1)
- {
- //KeyValue_Pro();
- if((Page_Flag1==1)&&(Page_Flag !=0))
- {
- Page_Flag1 = 0;
- LCD_CLS();
- }
- if(Cls_Lcd_Flag)
- {
- Cls_Lcd_Flag = 0;
- Page_Flag = 0;
- LCD_CLS();
- Up_Counter = 1;
- }
-
- Menu_Display();
- /*
- if(temp_flag && (Page_Flag == 0)) // 由于溫度檢測測量時間較長影響按鍵和通訊,只有在溫度顯示頁面才進行溫度刷新
- {
- Temp_Humi_Init();
- temp_flag=0;
- }
- */
- Out_Logic();
- Check_IO_Pra();
- if(Receiver_Flag)
- {
- shuju_chuli();
- clear_rx0_tx0_buf();
- Receiver_Flag = 0;
- …………
- …………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
|