Salutare.
Am nevoie de putin ajutor in modificare unui cod C pentru un ceas folosind un microcontroler Atmega 16 p.
Trebuie sa configurez 3 butoare ( Set , Up, Down ) si nu prea reusesc .
Aici am schema electrica :
schema.pdf (18.1 KB)
#define F_CPU 4000000UL
#include <avr/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#define SegDataPort PORTB
#define SegDataPin PINB
#define SegDataDDR DDRB
#define SegCntrlPort PORTC
#define SegCntrlPin PINC
#define SegCntrlDDR DDRC
unsigned char hours = 0;
unsigned char minutes = 0;
unsigned char seconds = 0;
unsigned char DigitTo7SegEncoder(unsigned char digit, unsigned char common);
ISR(TIMER1_COMPA_vect);
int main(void)
{
SegDataDDR = 0xFF;
SegCntrlDDR = 0x3F;
SegCntrlPort = 0xFF;
TCCR1B = (1<<CS12|1<<WGM12);
OCR1A = 15625-1;
TIMSK = 1<<OCIE1A;
sei();
while(1)
{
if((SegCntrlPin & 0x40) == 0 )
{
_delay_ms(200);
if(minutes < 59)
minutes++;
else
minutes = 0;
}
if((SegCntrlPin & 0x80) == 0 )
{
_delay_ms(200);
if(hours < 23)
hours++;
else
hours = 0;
}
SegDataPort = DigitTo7SegEncoder(seconds%10,1);
SegCntrlPort = ~0x01;
SegDataPort = DigitTo7SegEncoder(seconds/10,1);
SegCntrlPort = ~0x02;
SegDataPort = DigitTo7SegEncoder(minutes%10,1);
SegCntrlPort = ~0x04;
SegDataPort = DigitTo7SegEncoder(minutes/10,1);
SegCntrlPort = ~0x08;
SegDataPort = DigitTo7SegEncoder(hours%10,1);
SegCntrlPort = ~0x10;
SegDataPort = DigitTo7SegEncoder(hours/10,1);
SegCntrlPort = ~0x20;
}
return 0;
}
unsigned char DigitTo7SegEncoder(unsigned char digit, unsigned char common)
{
unsigned char SegVal;
switch(digit)
{
case 0: if(common == 1) SegVal = 0b00111111;
else SegVal = ~0b00111111;
break;
case 1: if(common == 1) SegVal = 0b00000110;
else SegVal = ~0b00000110;
break;
case 2: if(common == 1) SegVal = 0b01011011;
else SegVal = ~0b01011011;
break;
case 3: if(common == 1) SegVal = 0b01001111;
else SegVal = ~0b01001111;
break;
case 4: if(common == 1) SegVal = 0b01100110;
else SegVal = ~0b01100110;
break;
case 5: if(common == 1) SegVal = 0b01101101;
else SegVal = ~0b01101101;
break;
case 6: if(common == 1) SegVal = 0b01111101;
else SegVal = ~0b01111101;
break;
case 7: if(common == 1) SegVal = 0b00000111;
else SegVal = ~0b00000111;
break;
case 8: if(common == 1) SegVal = 0b01111111;
else SegVal = ~0b01111111;
break;
case 9: if(common == 1) SegVal = 0b01101111;
else SegVal = ~0b01101111;
}
return SegVal;
}
ISR(TIMER1_COMPA_vect)
{
seconds++;
if(seconds == 60)
{
seconds = 0;
minutes++;
}
if(minutes == 60)
{
minutes = 0;
hours++;
}
if(hours > 23)
hours = 0;
}