16段数码管原理同8段的一样,只不过要注意对应的关系:
Proteus 仿真截图:
Atmel Studio6.2编译通过截图:
程序清单:
/*
* GccApplication24.c
*
* Created: 2014-11-28 20:43:33
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
const uint16_t SEG_CODR16[] = {0xff00,0xfff3,0x7788,0x77c0,0x7773,0x7744,0x7704,0xFFF0,
0x7700,0x7740,
0x7730,0x7304,0xff0c,0xddc0,0x770c,0x773c,0xf704,0x7733,0xddcc,0xdd9c,
0x6b3f,0xff0f,0xfa33,0xee33,0xff00,0x7738,0xef00,0x6738,0x7744,0xddfc,
0xff03,0xbb3f,0xaf33,0xaaff,0xfaff,0xbbcc};
uint8_t str_buffer[] = "DISTabcdefghijklmnopqrstuvwxyz";
uint16_t get_16_segcode(char c)
{
if(isdigit(c))
{
c = c - '0';
return SEG_CODR16[(uint8_t)c];
}
else if(isalpha(c))
{
c = toupper(c) - 'A' + 10;
return SEG_CODR16[(uint8_t)c];
}
else return 0xFFFF;
}
int main(void)
{
int i,j,len = strlen(str_buffer);
uint8_t pre_key = 0xFF;
uint16_t sCode = 0x0000;
DDRA = 0x00; PORTA = 0xFF;
DDRB = 0xFF;
DDRC = 0xFF;
DDRD = 0xFF;
while(1)
{
i = 0;
while(i<ceil(len/8.0))
{
for(j=0;j<8 && 8*i+j<len;j++)
{
PORTB = 0xFF;
sCode =~get_16_segcode(str_buffer[8*i+j]);
PORTD = sCode >> 8;
PORTC = sCode &0x00FF;
PORTB = ~_BV(j);
_delay_ms(4);
}
if(PINA != pre_key)
{
if(PINA!=0xFF)i++;
pre_key = PINA;
}
}
}
}
|