Cours:TP printempsM4209 TP 5 Corr
Révision datée du 8 février 2016 à 17:24 par SergeMoutou (discussion | contributions) (Page créée avec « <accesscontrol>Acces:Prof</accesscontrol> <source lang=c> #include "avr/io.h" #undef F_CPU #define F_CPU 15000000UL #include "util/delay.h" //#define UCSRB _SFR_IO8(0x01... »)
Il s’agit d’une page protégée.
#include "avr/io.h"
#undef F_CPU
#define F_CPU 15000000UL
#include "util/delay.h"
//#define UCSRB _SFR_IO8(0x01)
//#define UCSRA _SFR_IO8(0x02)
//#define UDR _SFR_IO8(0x03)
// UCSRA
//#define RXC 7
//#define TXC 6
//#define UDRE 5
//UCSRB
//#define RXEN 4
//#define TXEN 3
//const unsigned char digit7segs[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
void incrementBCD(unsigned char *cnt);
void decrementBCD(unsigned char *cnt);
//***********************************************************************
// main
//***********************************************************************
int main (void) {
//unsigned char transcod7segs[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0xFF,0x00};
//unsigned char transcod7segs[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
unsigned char cmpt=0,ch=128,swPresent=0,swPasse=0,etat=1;
while(1) {
// compteur simple
// PORTB = transcod7segs[cmpt&0x0F];
// DDRB = transcod7segs[(cmpt&0xF0)>>4];
PORTB= cmpt;
swPresent = PINA;
switch (etat) {
case 0x01 : if (((swPresent & 0x03)==0x01) && ((swPasse & 0x01)==0x00)) {etat = 0x02;break;}
if (((swPresent & 0x03)==0x02) && ((swPasse & 0x02)==0x00)) {etat = 0x08;break;}
case 0x02 : if (((swPresent & 0x03)==0x02) && ((swPasse & 0x02)==0x00)) etat = 0x04;break;
case 0x04 : etat = 0x01; break;
case 0x08 : if (((swPresent & 0x03)==0x01) && ((swPasse & 0x01)==0x00)) etat = 0x10;break;
case 0x10 : etat = 0x01; break;
default : etat = 0x01;
}
if (etat==0x04) incrementBCD(&cmpt);
if (etat==0x10) decrementBCD(&cmpt);
swPasse = swPresent;
PORTA = ch;
ch >>= 1;
if (ch == 0) ch = 128;
_delay_ms(300); // on verra passer les caractères
}
return 0;
}
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;
}