赞
踩
逻辑部分纯手写简单 零基础模板套用即可
main.c
- #include "smg.h"
- #include "key.h"
- #include "led.h"
- #include "iic.h"
- #include "onewire.h"
- #include "ds1302.h"
- #include "timer.h"
- #include "uart.h"
- #include "ult.h"
- uchar display_state;
- uchar LED[] = {0,0,0,0,0,0,0,0};
- uchar SMG[] = {10,10,10,10,10,10,10,10};
- uchar dot[] = {0,0,0,0,0,0,0,0};
- uchar distance_shezhi = 30;
- uchar light_value;
- uint temp;
- uchar state;
- uchar distance_now;
- void led_pro()
- {
- LED[0] = (display_state == 0)?1:0;
- LED[1] = (display_state == 1)?1:0;
- LED[2] = (distance_now > distance_shezhi)?state:0;
- }
-
- void smg_pro()
- {
- if(display_dly<200)return;
- display_dly = 0;
- distance_now = read_distance();
- switch(display_state)
- {
- case 0:
- SMG[0] = 11;
- SMG[1] = 1;
- SMG[2] = 10;
- SMG[3] = 10;
- SMG[4] = 10;
- SMG[5]=(distance_now<100)?10:distance_now/100;
- SMG[6]=(distance_now<10)?10:distance_now/10%10;
- SMG[7]= distance_now %10;
- break;
- case 1:
- SMG[0] = 11;
- SMG[1] = 2;
- SMG[2] = 10;
- SMG[3] = 10;
- SMG[4] = 10;
- SMG[5] = distance_shezhi / 100;
- SMG[6] = distance_shezhi / 10 % 10;
- SMG[7] = distance_shezhi % 10;
- }
- }
-
- void key_pro()
- {
- uchar key;
- if(key_dly < 10) return;
- else key_dly = 0;
- key = key_scan2();
- switch(key)
- {
- case 13:
- display_state ++ ;
- if(display_state == 2)
- display_state = 0;
- break;
- case 14:
- if(display_state == 0)
- distance_shezhi = distance_now;
- break;
- case 15:
- if(display_state == 1)
- distance_shezhi = distance_shezhi + 10;
- break;
- case 16:
- if(display_state == 1)
- {
- if(distance_shezhi >= 10)
- distance_shezhi = distance_shezhi - 10;
- else if(distance_shezhi < 10)
- distance_shezhi = 0;
- }
- break;
- case 10:
- printf("Distance:%dcm\r\n",(uint)distance_now);
- break;
- }
-
- }
-
-
- void main()
- {
- system_init();
- Timer1Init();
- EA = 1;
- UartInit();
- while(1)
- {
-
- led_pro();
- key_pro();
- smg_pro();
-
- }
- }
smg.c
- #include "smg.h"
- code segment[] = {
- // 0 1 2 3 4 5 6 7 8 9 熄灭 U
- 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F, 0x00,0x3E
- };
-
- void smg(uchar * temp,uchar *dot,uchar pos)
- {
- P0=0xff;
- hc573(7);
- P0=0x01<<pos;
- hc573(6);
- if(dot[pos]==0)
- P0=~segment[temp[pos]];
- else
- P0=(~segment[temp[pos]]) & 0x7f;
- hc573(7);
- }
led.c
- #include "led.h"
-
-
- void led(uchar * LED,uchar pos)
- {
- static uchar temp=0xff;
- if(LED[pos])
- temp &= ~(0x01<<pos);
- else
- temp |= 0x01<<pos;
- P0=temp;
- hc573(4);
- }
timer.c
- #include "timer.h"
-
- unsigned long systick_ms;
- uchar state_relay;//继电器控制
- uchar pos; //数码管led扫描
- uchar key_dly; //按键调度
- uint display_dly,collect_dly; //显示调度,采集调度
- uint fre; //频率变量
- uint count; //1s计时
- void Timer0Init(void) //100微秒@12.000MHz
- {
- AUXR &= 0x7F; //定时器时钟12T模式
- TMOD &= 0xF0; //设置定时器模式
- TL0 = 0x9C; //设置定时初值
- TH0 = 0xFF; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0 = 1;
- }
-
- void Timer0Init_Count(void) //计数模式
- {
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0x05; //16位不自动重装
- TL0 = 0; //设置定时初值
- TH0 = 0; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计数
- ET0 = 1;
- }
-
- void Timer1Init(void) //1毫秒@12.000MHz
- {
- AUXR &= 0xBF; //定时器时钟12T模式
- TMOD &= 0x0F; //设置定时器模式
- TL1 = 0x18; //设置定时初值
- TH1 = 0xFC; //设置定时初值
- TF1 = 0; //清除TF1标志
- TR1 = 1; //定时器1开始计时
- ET1 = 1;
- }
-
- void timer0() interrupt 1
- {
-
- }
-
- void timer1() interrupt 3
- {
- if(++count == 200)
- {
- state = ~state;
- count=0;
- }
- systick_ms++;
- key_dly++;
- display_dly++;
- collect_dly++;
- smg(SMG,dot,pos);
- led(LED,pos);
- if(++pos == 8) pos = 0;
- }
ult.c
- #include "ult.h"
- sbit TX = P1^0; // 发射引脚
- sbit RX = P1^1; // 接收引脚
-
- uchar read_distance(void)
- {
- uchar distance,num = 10;
- TX = 0;
- CL = 0xF3; // 设置定时初值
- CH = 0xFF; // 设置定时初值
- CR = 1; // 定时器0计时
- // TX引脚发送40KHz方波信号驱动超声波发送探头
- while(num--)
- {
- while(!CF);
- TX ^= 1;
- CL = 0xF3; // 设置定时初值
- CH = 0xFF; // 设置定时初值
- CF = 0;
- }
- CR = 0;
- CL = 0; // 设置定时初值
- CH = 0; // 设置定时初值
- CR = 1;
- while(RX && !CF); // 等待收到脉冲
- CR = 0;
- if(CF) // 发生溢出
- {
- CF = 0;
- distance = 255;
- }
- else // 计算距离
- distance = ((CH<<8)+CL)*0.017;
- return distance;
- }
sys.c && delay.c
- #include "sys.h"
-
- void hc573(uchar channel)
- {
- switch(channel)
- {
- case 4: P2 = P2 & 0x1f | 0x80; break;
- case 5: P2 = P2 & 0x1f | 0xa0; break;
- case 6: P2 = P2 & 0x1f | 0xc0; break;
- case 7: P2 = P2 & 0x1f | 0xe0; break;
- }
- P2 = P2 & 0x1f;
- }
-
- void system_init()
- {
- P0 = 0x00;
- hc573(5);
- P0 = 0xff;
- hc573(4);
- }
-
- #include "delay.h"
-
- void Delayms(uint xms) //@12.000MHz
- {
- unsigned char i, j;
- while(xms--)
- {
- i = 12;
- j = 169;
- do
- {
- while (--j);
- } while (--i);
- }
- }
-
- void Delayus(uint xus) //@12.000MHz
- {
- while(xus--)
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- }
uart.c
- #include "uart.h"
-
- uchar Uart_Rxindex;
- uchar Uart_Rxbuf[10];
- void UartInit(void) //4800bps@12.000MHz
- {
- SCON = 0x50; // 8位数据,可变波特率
- AUXR |= 0x01; // 串口1选择定时器2为波特率发生器
- AUXR |= 0x04; // 定时器2时钟为Fosc, 即1T
- T2L = 0x8F; // 设定定时初值
- T2H = 0xFD; // 设定定时初值
- AUXR |= 0x10; // 启动定时器2
- ES = 1; // 允许串口中断
- }
-
- void Uart0(void) interrupt 4
- {
- if(RI)
- {
- if(Uart_Rxindex == 10)
- Uart_Rxindex = 0;
- Uart_Rxbuf[Uart_Rxindex++] = SBUF;
- RI = 0;
- }
- }
- char putchar(char ch)
- {
- SBUF = ch;
- while (TI == 0); // 等待发送完成
- TI = 0; // 发送完成标志位置0
- return ch;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。