Cours:TP printempsM4209 TP 4 Corr
Exercice 2
On a gardé le chenillar de l'exercice 1 et ajouté le compteur BCD :
#include "avr/io.h"
#undef F_CPU
#define F_CPU 15000000UL
#include "util/delay.h"
void incrementBCD(unsigned char *cnt) {
(*cnt)++;
if ((*cnt & 0x0F) > 0x09) *cnt += 6;
if ((*cnt & 0xF0) > 0x90) *cnt = 0;
}
void decrementBCD(unsigned char *cnt) {
(*cnt)--;
if ((*cnt & 0x0F) == 0x0F) *cnt -= 6;
if ((*cnt & 0xF0) == 0xF0) *cnt = 0x99;
}
//***********************************************************************
// main
//***********************************************************************
int main (void) {
unsigned char ch=128, cmpt = 0;
const unsigned char digit7segs[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
while(1) {
// echo simple
PORTA = ch;
ch >>= 1;
if (ch == 0) ch = 128;
if (PINA & 0x01) decrementBCD(&cmpt); else incrementBCD(&cmpt);
PORTB = digit7segs[cmpt & 0x0F];
DDRB = digit7segs[(cmpt>>4) & 0x0F];
_delay_ms(1000); // on verra passer les caractères
}
return 0;
}