数码管显示实验

来源:互联网 发布:吃鸡优化软件官网 编辑:程序博客网 时间:2024/05/17 23:02

实验目的:

通过实验,掌握如何通过74HC595扩展IO口,掌握如何运用动态扫描的方法驱动多位数码管;

实验内容:

判断按键SW8(INT1)是否按下,并对按键次数计数,显示在数码管之上;(单片机刚启动后,数码管显示为全0,当第一次按下SW8后,数码管显示0001,当第二次按下SW8后,数码管显示0002)


代码实现:

//ICC-AVR application builder : 2015-4-1 下午 14:28:10// Target : M16// Crystal: 7.3728Mhz#include <iom16v.h>#include <macros.h>#define CLR_SHCLK()  PORTB&=~(1<<1)  //移位时钟 SCLK#define SET_SHCLK()  PORTB|=(1<<1)  //移位时钟 SCLK,上升沿#define CLR_STCLK()  PORTB&=~(1<<0)  //锁存时钟 RCLK#define SET_STCLK()  PORTB|=(1<<0)  //拉高锁存时钟,上升沿#define CLR_DS()PORTA&=~(1<<0)  //清零#define SET_DS()PORTA|=(1<<0)  //置位#define uchar unsigned char#define uint unsigned int//定义字符表    unsigned char led_7[14]={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,    0x80,0x90,0xff,0x7f,0xc6,0x00};uchar led_buf[4]={0,0,0,0}; unsigned int count=0;void Delay_mS(unsigned int time) //延时函数{    unsigned char n;    while(time>0)    {        for (n=1;n<187;n++)        {            asm("nop");        }        time--;    }} void port_init(void) //初始化IO{    PORTA = 0x00;    DDRA  = 0x01;    PORTB = 0x00;    DDRB  = 0x03;    PORTC = 0x00; //m103 output only    DDRC  = 0x00;    PORTD = 0x00;    DDRD  = 0xF0;    DDRD &=~(1<<3);    PORTD|=(1<<3); }void Hc595send(unsigned char SndData) {     uchar i;    for(i=0;i<8;i++)    {          if(SndData&(1<<(7-i))){            SET_DS();        }else{            CLR_DS();        }CLR_SHCLK();  //移位时钟 SCLK        SET_SHCLK();  //移位时钟 SCLK,上升沿    }CLR_STCLK();  SET_STCLK();  //拉高锁存时钟,上升沿}//call this routine to initialize all peripheralsvoid init_devices(void){    //stop errant interrupts until set up    CLI(); //disable all interrupts    port_init();    MCUCR = 0x00;    GICR  = 0x00;    TIMSK = 0x00; //timer interrupt sources    SEI(); //re-enable interrupts    //all peripherals are now initialized}void handle_data(uint count,uchar led_buf[]) //处理数据{    led_buf[0]=count/1000;    led_buf[1]=(count/100)%10;    led_buf[2]=count/10%10;    led_buf[3]=count%10;}void display(void) 显示函数{    uchar i;    for(i=0;i<4;i++)    {Hc595send(led_7[led_buf[i]]);PORTD &=~(1<<7-i);Delay_mS(10);PORTD|=0xff;    }}void get_key(void) 按键函数{    if((PIND&(1<<3))==0){    display();display();if((PIND&(1<<3))==0){    while((PIND&(1<<3))==0)    display();    count++;    if(count>9999) count=0;    handle_data(count,led_buf);}}}int main(void){    init_devices();    //PORTD &=~(0xf0<<4);    //Hc595send(0xA4);    handle_data(count,led_buf);    while(1)    { get_key(); display();    }    return 0;}




0 0