Introduction
In this note I make a program that counts decimal number from 00.0 to 99.99. Eventually, this program will be the base program to both a voltmeter and a thermometer. That's actually the same thing. Calculating the voltage is a tiny bit different.
The program
#include <xc.h>
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA4/OSC2/CLKOUT and RA5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Selection bits (BOR disabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#define _XTAL_FREQ 8000000
#define digit1 PORTBbits.RB4
#define digit2 PORTBbits.RB5
#define digit3 PORTBbits.RB6
unsigned char binary_pattern[]={0b00111111,0b00000110,0b01011011,0b01001111,0b01100110,0b01101101,0b01111101,0b00000111,0b01111111,0b01101111};
unsigned char binary_pattern_dp[10]= {0b10111111,0b10000110,0b11011011,0b11001111,0b11100110,0b11101101,0b11111101,0b10000111,0b11111111,0b11101111}; // with dp turn on
unsigned int a1, a2, a3;
unsigned int counter = 0;
void main(void) {
CM1CON0 = 0;
CM2CON0 = 0;
PORTB = 0b00000000;
TRISB = 0b00000000;
PORTC = 0b00000000;
TRISC = 0b00000000;
ANSEL = 0;
ANSELH = 0;
digit1 = 0; // Decimal
digit2 = 0; // One's
digit3 = 0; // Ten's
while (counter < 9999) {
a1 = counter / 1000;
a2 = ((counter/100)%10);
a3 = ((counter/10)%10);
counter++;
// Decimal number
PORTC = binary_pattern[a3];
digit1=1;
__delay_ms(5);
digit1=0;
// One's number
PORTC = binary_pattern_dp[a2];
digit2 = 1;
__delay_ms(5);
digit2 = 0;
// Ten's number
PORTC = binary_pattern[a1];
digit3 = 1;
__delay_ms(5);
digit3 = 0;
}
return;
}
Work in progress