Counter Project recoded in C
So i've decided to rewrite the code in C language using the MikroC compiler for the Counter project.
I have chosen MikroC compiler for its easy of use and good documentation, unfortunatelly the IDE editor simple sucks big time, so it really is good for me to tolerate it.
I also got recently some professionally finished PCB's.
This is a part of the Counter project.
You can see a video of it here.
Now let's go for the code :
// Connect buttons and displays on PORTB pins #define DECIMALS PORTB.RB7 #define UNITS PORTB.RB6 #define BUP PORTB.RB4 #define BDOWN PORTB.RB3 typedef unsigned char uint8_t; char lower = 0; // minimum the counter can go char upper = 99; // maximum the counter can go char count = 0; // current count char display_converter (char number); // Converts decimal to 7 segments void interrupt() { PIR1.TMR2IF = 0; // clear Timer2 Interrupt flag, this will allow // for this same interrupt to occur again if (DECIMALS == 1) { // If decimals display ON PORTA = display_converter(count % 10); // send units digit to PORTA DECIMALS = 0; // turn OFF decimals digit UNITS = 1; // turn ON units digit } else { // else meaning units display is ON PORTA = display_converter(count / 10); // send decimals digit to PORTA DECIMALS = 1; // turn ON decimals digit UNITS = 0; // turn OFF units digit } } void main() { TRISA = 0; // set directions to be output TRISB = 0b00011000; // bit 4 and 3 PORTB are inputs ANSEL = 0; // set all pins to digital I/O OSCCON = 0x70; // Select 8 MHZ internal Oscillator PIE1.TMR2IE = 1; // enable Timer2 interrupt T2CON = 0x04; // INTCON.PEIE = 1; // INTCON.GIE = 1; // Enable global interrupts OPTION_REG = 0b00001111; // Assign 1:128 prescaler to WDT count = EEPROM_Read(0x10); // read from memory last // value before power-off while (1) { asm CLRWDT; if (!BUP) { // if Button UP activated Delay_ms(80); // small delay to slow down incrementing count++; // increment count if (count > upper) { // if its over the maximum count = lower; // set counter to minimum } EEPROM_Write(0x10, count); // save count into EEPROM location 0x10 } if (!BDOWN) { // if Button DOWN activated Delay_ms(80); // small delay to slow down decrementing if (count == 0) { // if count is zero count = upper; // set count to the maximum } else { // otherwise count--; // decrement count } EEPROM_Write(0x10, count); // save count into EEPROM location 0x10 } } } uint8_t display_converter (char number) { char digit[10] = { 0x60, // 0 0xEB, // 1 0xA4, // 2 0xA1, // 3 0x2B, // 4 0x31, // 5 0x30, // 6 0xE3, // 7 0x20, // 8 0x23 // 9 }; return digit[number]; }
Here you can find the PROJECT.