This what I wrote so far
It compiles and programs but as you know there is no output yet.
Needs to make sure if I got this right.
Rich (BB code):
Indicators are CC 7 Segments Muxed(PortD).
3 Digits for 0.00 to 8.40 Volts
3 Digits for 0.00 to 5.00 Amps
3 Digits for 2.00 Hour Timer
LED's are connected as CC and Muxed with the 7 segments and controlled by RE1
(anodes are connected as indicated below)
There are total of 5 Led indicators
1. Charging / Charge completed (blinks when Charging) - RD0
2. Constant Voltage mode - RD1
3. Constant Current mode - RD2
4. Battery inserted indicator - RD3
5. Battery Over heat indicator - RD4
==============================================================================
*Pin Connections:
RE3 is the MCLR
*PortA Connections
RA0 - Voltage ADC input
RA1 - Current ADC input
RA2 - Battery temperature ADC input
RA3 - Heat Sink temperature ADC input
RA4 - Buzzer
RA5 - Not used
RA6 - HS
RA7 - HS
*PortB Connections
RB0 - Charger Relay
RB1 - Battery Voltage Checker Relay
RB2 - 4.2V or 8.4V Selector Relay
RB3 - Charge Start/Halt/Stop Switch
RB4 - Battery Insert Switch
RB5 - Selector Sw, Voltage & Charge Termination Current & Timer (works with encoder)
RB6 - RotaryEncoder input A
RB7 - RotaryEncoder input B
*PortC + RE1 & RE2 are the digit/LED drivers:
RC0 - Digit 1(Timer Hr digit)
RC1 - Digit 2(Timer Min digit)
RC2 - Digit 3(Timer Min digit)
RC3 - Digit 4(Voltmeter digit)
RC4 - Digit 5(Voltmeter digit)
RC5 - Digit 6(Voltmeter digit)
RC6 - Digit 7(Ammeter digit)
RC7 - Digit 8(Ammeter digit)
RE2 - Digit 9(Ammeter digit)
RE1 - LED Drive
*PortE
RE0 - Fan Diver(heatsink)
*PortD are the segment drives:
RD0 - Segment A
RD1 - Segment B
RD2 - Segment C
RD3 - Segment D
RD4 - Segment E
RD5 - Segment F
RD6 - Segment G
RD7 - Decimal point
==============================================================================
Configuration bits set ny compiler :
CONFIG1 0x2FF2
CONFIG2 0x0600
=============================================================================*/
// Control bit assignement
sbit Bzr at RA7_bit; //Buzzer at RA7
sbit ChargerRelay at RB0_bit; //Charge ON Relay at RB0
sbit VtestRelay at RB1_bit; //Battery Voltage test Relay at RB1
sbit VselectRelay at RB2_bit; //Charge Voltage selector Relay at RB2
sbit StartSw at RB3_bit; //Start/Halt/Terminate (Charge) Sw at RB3
sbit BatIn at RB4_bit; //Battery inserted Sw at RB4
sbit EncdSw at RB5_bit; //Encoder Sw at RB5
sbit EncdA at RB6_bit; //Encoder input A at RB6
sbit EncdB at RB7_bit; //Encoder input B at RB7
sbit Fan at RE0_bit; //Fan Control out at RE0
/*********** Defines ******************************/
//Segments on Ports
#define SegA (0x01) //Segment A on Port0
#define SegB (0x02) //Segment B on Port1
#define SegC (0x04) //Segment C on Port2
#define SegD (0x08) //Segment D on Port3
#define SegE (0x10) //Segment E on Port4
#define SegF (0x20) //Segment F on Port5
#define SegG (0x40) //Segment G on Port6
#define SegDP (0x80) //Segment dp on Port7
//Digits Make up
#define Digit0 (SegA + SegB + SegC + SegD + SegE + SegF)
#define Digit1 (SegA + SegB)
#define Digit2 (SegA + SegB + SegD + SegE + SegG)
#define Digit3 (SegA + SegB + SegC + SegD + SegG)
#define Digit4 (SegB + SegC + SegF + SegG)
#define Digit5 (SegA + SegC + SegD + SegF + SegG)
#define Digit6 (SegA + SegC + SegD + SegE + SegF + SegG)
#define Digit7 (SegA + SegB + SegC)
#define Digit8 (SegA + SegB + SegC + SegD + SegE + SegF + SegG)
#define Digit9 (SegA + SegB + SegC + SegD + SegF + SegG)
#define DP
//Segment Look-up table
const unsigned char SegTable[] = {Digit0,Digit1,Digit2,Digit3,Digit4,Digit5,\
Digit6,Digit7,Digit8,Digit9,DP};
//Segment Buffer - Mux will fetch these Segments patterns in Seq.
#define N_DIGITS 10
unsigned char DigitsBuf[N_DIGITS];// 10 Digits including LED's
//Display Map - 3 Groups of Digits into Buffer
#define dTime 0 //Digits 0-2 are time
#define dVolts 3 //Digits 3-5 are Volts
#define dAmps 6 //Digits 6-8 are Current
#define dLeds 9 //LED's are offset 9, i.e 10th Digit
//Variables
unsigned int Volts,Amps,Time;
/*********** Timer Init ***************************/
/* 2 Timers are used
TMR0 is used to Muxing the 7 Segments and Leds, set at 5ms
TMR0 set is 256-(5ms/0.5usTcyc)/64Prescale = 256-156.25 counts = 99.75; almost 100
TMR1 is ued to generate System Tick */
void Timers(){
INTCON = 0; //No Interrupts
//Timer0 Interrupt Time is like 4.992 ms (close to 5ms - so good enuf)
OPTION_REG = 0x85; //Option Reg; Pull ups disable, Prescale to Timer0 & 1:64
//Timer1 Interrupt Time is 10 ms
T1CON = 0x01; //Timer1 control; 1:1 prescale & Enable timer
TMR1H = 0xB1; //Load the Timer1 MSB &
TMR1L = 0xE0; //load the Timer1 LSB
TMR0 = 100; //Load Timer0
TMR1IF_bit = 0; //PIR1 Reg; Clear the Timer1 overflow Interrupt flag bit
CCP1IF_bit = 0; //Clear Timer1 Interrupt flag
//Then Enable Interrupts
TMR1IF_bit = 1;
CCP1IF_bit = 1;
T1CON.TMR1ON = 1; //Start timer
INTCON.PEIE = 1; //Enable Peripheral Interrupts
INTCON.GIE = 1; //Enable Global Interrupts
}
/*********** Interrupt Service Routine ***************/
void Interrupt(){
//Used for Display Muxing.
if (TMR0IF_bit){
TMR0IF_bit = 0; //Ack IRQ
TMR0 = 100; //Reset Timer0
//Code goes here
}
//System Tik = 10ms
if (CCP1IF_bit){ // Interrupt on Timer1 = CCP
CCP1IF_bit = 0; //Ack IRQ
TMR1H = 0xB1; //Load the Timer1 MSB &
TMR1L = 0xE0; //load the Timer1 LSB
//Code goes here
}
}
//Conversion routine
void Int2Segs(unsigned int i, short ofs){
short x;
x = i/100;
DigitsBuf[ofs] = SegTable[x];
x = (i%100)/10;
DigitsBuf[ofs+1] = SegTable[x];
x = (i%10);
DigitsBuf[ofs+2] = SegTable[x];
}
/*********** Main Routine ****************************/
void main() {
PORTA = 0; // Clear PortA
ANSEL = 0x0F; // RA0 to RA3 as Analog Inputs
ANSELH = 0x00; // Rest are Digital I/O's
CM1CON0.C1ON = 0; // Disable Comparator 1
CM2CON0.C2ON = 0; // Disable Comparator 2
TRISA = 0x7F; // All Inputs except RA7 as Output
PORTB = 0; // Clear PORTS
TRISB = 0xF8; // RB <7:3> Inputs & RB <2:0> Ouputs
PORTC = 0; // Clear PORTS
TRISC = 0x00; // PORTC are Ouputs
PORTD = 0; // Clear PORTS
TRISD = 0x00; // PORTD are Ouputs
PORTE = 0; // Clear PORTS
TRISE = 0x00; // PORTE are Ouputs (MCLR is an input)
Timers(); //Initiate Timers
Time = 123;
Volts = 456;
Amps = 789;
while(1){
Int2Segs(Time,dTime);
Int2Segs(Volts,dVolts);
} Int2Segs(Amps,dAmps);
}
Needs to make sure if I got this right.
Last edited: