开始学习单片机。从pic学起。
想写一个时钟程序(为调试方便,只有秒、分)。4位数码管实现。
奈何调了好几天,没效果。
主要是那个1秒不知道怎么实现。已经有点明白为什么最后一行的那个1秒得延时是行不同的,它严重影响了显示。
proteus仿真文件
ourdev_648355TQAV6O.rar(文件大小:18K) (原文件名:led.rar)
代码如下,请各位高手指点一二。
#include <16f690.h>
//#define debug
#device ICD=TRUE
#fuses intrc, NOBROWNOUT, NOWDT, //DEBUG
#use delay(clock=4mhz)
#use fast_io(c)
#use fast_io(b)
//#setup_time_0(rtcc_div_1 | rtcc_internal | rtcc_8_bit)
//set_timer0();
int8 SEG[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x58,0x5E,0x7B,0x71,0x5C,0x73,0x3E,0x50};
//seg code // 0 1 2 3 4 5 6 7 8 9 A b c d e F o P U r
int8 stuf[] = {0x00,0x00,0x00,0x00,0x00,0x00}; //缓冲
int8 K[] = {0xe0,0xd0,0xb0,0x70}; //位选
//LED
//display
void led_disp(int8 m, int8 s)
{
int8 i;
// stuf[] = {m%10,m/10,s%10,s/10};
stuf[0] = m/10; // BCD
stuf[1] = m%10;
stuf[2] = s/10;
stuf[3] = s%10;
for(i=0;i<4;i++)
{
output_c(SEG[stuf
]); //段选
output_b(K);//位选
delay_ms(1);//延时
}
output_b(0xf0);//消隐
delay_ms(15);//
}
void main()
{
set_tris_c(0x00); //set PORTC for output
set_tris_b(0x00); //set PORTA for output
int8 m=10,s=20;
while(1)
{
if(s==20)
{
s = 0;
m++;
}
if(m==10)
m = 0;
led_disp(m,s);//
s++;
delay_ms(1000);//1秒
}
}
it is very simple.
start with a piece of code that display a number on 1 7-segment led display;
then use that to build a routine that display a 4-digit number on 4 7-segment display;
then learn to use interrupts, to generate delay, and to process input.
-----------------------------------------------------------------------
thinks.
回复【2楼】topdog
-----------------------------------------------------------------------
是否可以理解为这个 1秒 的实现需要用到中断?
一周热门 更多>