/*
my vm
_______________________________________________________________________________________
| _______ |
| vdd | 1 14 | Vss |
|_______RA5 | 2 13 | AN0 <-- ADC_0 |
________ RA4 | 3 12 | AN1 <-- ADC_1 |
| MCLR | 4 11 | RA2 ___________________________________________ | |
| ______ RC5 | 5 10 | RC0 ______ | | |
| | ____ RC4 | 6 9 | RC1 ____ | _______________ _______________ _______________
| | | __ RC3 | 7 8 | RC2 __ | | | CA ______ | | CC ______ | | CC ______ |
| | | | _______ | | | | F / A / | | F / A / | | F / A / |
| | | | | | | | /_____/ B | | /_____/ B | | /_____/ B |
| | | | | | | | E / G / | | E / G / | | E / G / |
| | | | | | | | /_____/ C | | /_____/ C | | /_____/ C |
| | | | | | | | D | | D | | D |
| | | | | | | |RA2=100 H | |RA2=10 H | |RA5=1 H |
| | | | | | | |_______D3______| |______D2_______| |_____D1________|
| | | | | | | A B C D E F G H A B C D E F G H A B C D E F G H
| | | | | | |__WWW___|_|_|_|_|_|_|_______|_|_|_|_|_|_|_______| | | | | | |
| | | | | |____WWW_____|_|_|_|_|_|_________|_|_|_|_|_|_________| | | | | |
| | | | |______WWW_______|_|_|_|_|___________|_|_|_|_|___________| | | | |
| | | |______________________________WWW_________|_|_|_|_____________|_|_|_|_____________| | | |
| | |________________________________WWW___________|_|_|_______________|_|_|_______________| | |
| |__________________________________WWW_____________|_|_________________|_|_________________| |
|____________________________________WWW_______________|___________________|___________________|
reff code form -> http://coolcircuit.com/project/meter/index.html
*/
#include <16F676.h>
#device adc=10
#fuses INTRC_IO,NOWDT,PUT,NOPROTECT,BROWNOUT,NOMCLR
#use delay (clock=4000000) // 4MHz clock
#rom 0x3ff={0x3444}
#byte PORTA = 0x05
#byte PORTC = 0x07
#byte TRISA = 0x85
#byte TRISC = 0x87
#define SPORTA PORTA
#define SPORTC PORTC
#define TICKS_BETWEEN_INTERRUPTS 5000 //5000
#define INTERRUPT_OVERHEAD 35
#define TMR1RESET (0xFFFF-(TICKS_BETWEEN_INTERRUPTS-INTERRUPT_OVERHEAD))
const char SegCode_CA[11] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0xFF};//for commen aNode
const char SegCode__CC[11] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x00}; //for commen cathode
// 0 1 2 3 4 5 6 7 8 9 OFF
const char Column[3] = ={ 0x24, 0x20, 0x04 };
static char Segment[3] = {0x7f,0x7f,0x7f};
static unsigned char ColCount=0x00;
void CPU_SETUP(void);
void Display(void);
void HTO7S(unsigned int32 Num);
byte i;
unsigned int32 result;
#INT_TIMER1
void Timer1(void)
{
set_timer1(TMR1RESET);
Display();
}
void main()
{
unsigned char i;
CPU_SETUP();
while(true)
{
result=0;
for (i=0;i<20;i++)
{
set_adc_channel(3);
delay_ms(1);
result=result+read_adc();
}
//result = 0x3fe;
HTO7S(result/20);
delay_ms(200);
}
}
void CPU_SETUP()
{
setup_comparator(NC_NC_NC_NC); // not use comparator module
setup_adc_ports( sAN3 | VSS_VDD);
setup_adc(ADC_CLOCK_DIV_64);
TRISA=0b00000011;
TRISC=0b00000000;
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(TMR1RESET);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER1);
}
//-------------------------------------
// Display routine
//-------------------------------------
void Display()
{
PORTA = 0b00100111; // off all digits column and Segment G
PORTC = 0b00111111; // off segment a-f
delay_cycles(2);
if (ColCount>=3)
ColCount=0;
SPORTC = Segment[ColCount];
SPORTA = ((Segment[ColCount] & 0b01000000)>>1) | (Column[ColCount]^0x07);
ColCount++;
}
//--------------------------------------
// Convet HEX 2 byte to 7-Segment code
//--------------------------------------
void HTO7S(unsigned int32 Num)
{
unsigned int32 res;
Segment[0]=SegCode_CA[30*Num/10230];
if (Segment[0]==0x40)
Segment[0]=0xFF;
res = 30*Num%10230;
Segment[1]=SegCode_CC[10*res/10230];
res=10*res%10230;
Segment[2]=SegCode_CC[10*res/10230];
}