作者 | strongerHuang
微信公众号 | 嵌入式专栏
介绍
源码
void PortSetup () {
for (int p=0; p<4; p++) Digit[p]->DIR = 0xFF; // All pins outputs
PORTE.DIR = PIN0_bm | PIN1_bm; // COMs outputs, PE0 and PE1
PORTF.DIR = PIN5_bm | PIN4_bm; // 1A, colon
}
ISR(RTC_PIT_vect) {
static uint8_t cycles = 0;
static unsigned long halfsecs;
RTC.PITINTFLAGS = RTC_PI_bm; // Clear interrupt flag
// Toggle segments
for (int p=0; p<4; p++) Digit[p]->OUTTGL = 0xFF; // Toggle all PORTA,B,C,D pins
PORTE.OUTTGL = PIN0_bm | PIN1_bm; // Toggle COMs, PE0 and PE1
PORTF.OUTTGL = PIN5_bm | PIN4_bm; // Toggle segment 1A, Colon
cycles++;
if (cycles < 32) return;
cycles = 0;
// Update time
halfsecs = (halfsecs+1) % 172800; // 24 hours
uint8_t ticks = halfsecs % 120; // Half-second ticks
if (MinsButton()) halfsecs = ((halfsecs/7200)*60 + (halfsecs/120 + 1)%60)*120;
if (HoursButton()) halfsecs = halfsecs + 7200;
if (MinsButton() || HoursButton() || ticks < 108) DisplayTime(halfsecs);
else if (ticks == 108) DisplayVoltage();
else if (ticks == 114) DisplayTemp();
}
void DisplayTime (unsigned long halfsecs) {
uint8_t minutes = (halfsecs / 120) % 60;
uint8_t hours = (halfsecs / 7200) % 12 + 1;
uint8_t hours = (halfsecs / 7200) % 24;
Digit[0]->OUT = Char[hours/10];
Digit[1]->OUT = Char[hours%10];
Digit[2]->OUT = Char[minutes/10];
uint8_t units = Char[minutes%10];
Digit[3]->OUT = units;
uint8_t colon = (halfsecs & 1)<<4; // Toggle colon at 1Hz
PORTF.OUT = (units>>1 & PIN5_bm) | colon;
}
void DisplayVoltage () {
ADC0.MUXPOS = ADC_MUXPOS_DACREF0_gc; // Measure DACREF0
ADC0.CTRLA = ADC_ENABLE_bm; // Single, 12-bit, left adjusted
ADC0.COMMAND = ADC_STCONV_bm; // Start conversion
while (ADC0.COMMAND & ADC_STCONV_bm); // Wait for completion
uint16_t adc_reading = ADC0.RES; // ADC conversion result
uint16_t voltage = adc_reading/50;
ADC0.CTRLA = 0; // Disable ADC
// Display it
Digit[0]->OUT = Char[Space];
Digit[1]->OUT = Char[voltage/10] | 0x80; // Decimal point
Digit[2]->OUT = Char[voltage%10];
uint8_t units = Char[Vee];
Digit[3]->OUT = units;
PORTF.OUT = (units>>1 & PIN5_bm); // No colon
}
开源软硬件资料