sbit Set_Or_Run_Button at RB0_bit;
sbit Increment_Button at RB1_bit;
sbit Decrement_Button at RB2_bit;
sbit LED at RD6_bit;
sbit RELAY at RD7_bit;
#define SSD_DATA_PORT PORTC
#define ON 1
#define OFF 0
#define SET 1
#define CLEAR 0
#define TRUE 1
#define FALSE 0
#define SET_OR_RUN_BUTTON_PRESSED (Set_Or_Run_Button == 0)
#define INCREMENT_BUTTON_PRESSED (Increment_Button == 0)
#define DECREMENT_BUTTON_PRESSED (Decrement_Button == 0)
unsigned char myFlags = 0, segment_index = 0, j = 0, digits[2] = {0, 0};
signed char delay_value_in_seconds = 0, previous_delay_value_in_seconds = 1, counter = 0;
unsigned int two_x_milli_second_counter = 0;
unsigned char cc_segment_mask[10][7] = {
{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00},
{0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x02, 0x40, 0x10, 0x08, 0x00, 0x00},
{0x01, 0x02, 0x40, 0x04, 0x08, 0x00, 0x00},
{0x20, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00},
{0x01, 0x20, 0x40, 0x04, 0x08, 0x00, 0x00},
{0x01, 0x20, 0x10, 0x08, 0x04, 0x40, 0x00},
{0x01, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40},
{0x01, 0x02, 0x04, 0x08, 0x20, 0x40, 0x00},
};
sbit mode_flag at myFlags.B0;
sbit count_down_flag at myFlags.B1;
#define SET_MODE (mode_flag == 1)
#define RUN_MODE (mode_flag == 0)
//Timer1
//Prescaler 1:1; TMR1 Preload = 61536; Actual Interrupt Time : 2 ms
//Place/Copy this part in declaration section
void InitTimer1() {
T1CON = 0x01;
TMR1IF_bit = 0;
TMR1H = 0xF0;
TMR1L = 0x60;
TMR1IE_bit = 1;
INTCON = 0xC0;
}
void Interrupt() {
if(TMR1IF_bit) {
//Enter your code here
SSD_DATA_PORT = 0x00;
PORTE = 0x07;
switch(segment_index) {
case 0:
SSD_DATA_PORT = cc_segment_mask[digits[j]][0];
break;
case 1:
SSD_DATA_PORT = cc_segment_mask[digits[j]][1];
break;
case 2:
SSD_DATA_PORT = cc_segment_mask[digits[j]][2];
break;
case 3:
SSD_DATA_PORT = cc_segment_mask[digits[j]][3];
break;
case 4:
SSD_DATA_PORT = cc_segment_mask[digits[j]][4];
break;
case 5:
SSD_DATA_PORT = cc_segment_mask[digits[j]][5];
break;
case 6:
SSD_DATA_PORT = cc_segment_mask[digits[j]][6];
break;
};
if((j == 0) && (digits[0] != 0)) {
PORTE.F0 = 0;
}
else if(j == 1) {
PORTE.F1 = 0;
}
else if(j > 1)j = 0;
if(++segment_index == 7) {
segment_index = 0;
if(++j > 1)j = 0;
}
if(count_down_flag) {
if(++two_x_milli_second_counter == 500) {
if(--delay_value_in_seconds == 0) {
RELAY = ON;
count_down_flag = CLEAR;
}
two_x_milli_second_counter = 0;
}
}
TMR1IF_bit = 0;
TMR1H = 0xF0;
TMR1L = 0x60;
}
}
void main() {
CMCON = 0x07;
ADCON1 = 0x87;
TRISA = 0x00;
TRISB = 0x07;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x07;
Delay_ms(100);
InitTimer1();
while(1) {
if(SET_OR_RUN_BUTTON_PRESSED) {
Delay_ms(30);
while(SET_OR_RUN_BUTTON_PRESSED);
mode_flag = ~mode_flag;
if(RUN_MODE) {
if(delay_value_in_seconds > 0) {
RELAY = OFF;
count_down_flag = 1;
}
else if(delay_value_in_seconds == 0) {
RELAY = ON;
}
}
else if(SET_MODE) {
count_down_flag = 0;
}
}
if(SET_MODE) {
if(INCREMENT_BUTTON_PRESSED) {
Delay_ms(120);
if(INCREMENT_BUTTON_PRESSED) {
if(++delay_value_in_seconds > 99) {
delay_value_in_seconds = 0;
}
}
}
else if(DECREMENT_BUTTON_PRESSED) {
Delay_ms(120);
if(DECREMENT_BUTTON_PRESSED) {
if(--delay_value_in_seconds < 0) {
delay_value_in_seconds = 99;
}
}
}
}
if(delay_value_in_seconds != previous_delay_value_in_seconds) {
if((delay_value_in_seconds >= 0) && (delay_value_in_seconds < 10)) {
digits[0] = 0;
digits[1] = delay_value_in_seconds;
}
else if((delay_value_in_seconds >= 10) && (delay_value_in_seconds < 100)) {
digits[0] = delay_value_in_seconds / 10;
digits[1] = delay_value_in_seconds % 10;
}
previous_delay_value_in_seconds = delay_value_in_seconds;
}
}
}
thanksTry this new method in hardware. See attached Proteus Simulation Video. It uses Segment Multiplexing and so only one segment of a digit will be ON at a time and each segment will draw 10 to 15 mA and so PIC Pin can directly drive the Common Cathode pin of the 7 Segment Display.
Again R8 value should be 330E 1/4W.
Here is the mikroC PRO PIC Code.
View attachment 112341Code:sbit Set_Or_Run_Button at RB0_bit; sbit Increment_Button at RB1_bit; sbit Decrement_Button at RB2_bit; sbit LED at RD6_bit; sbit RELAY at RD7_bit; #define SSD_DATA_PORT PORTC #define ON 1 #define OFF 0 #define SET 1 #define CLEAR 0 #define TRUE 1 #define FALSE 0 #define SET_OR_RUN_BUTTON_PRESSED (Set_Or_Run_Button == 0) #define INCREMENT_BUTTON_PRESSED (Increment_Button == 0) #define DECREMENT_BUTTON_PRESSED (Decrement_Button == 0) unsigned char myFlags = 0, segment_index = 0, j = 0, digits[2] = {0, 0}; signed char delay_value_in_seconds = 0, previous_delay_value_in_seconds = 1, counter = 0; unsigned int two_x_milli_second_counter = 0; unsigned char cc_segment_mask[10][7] = { {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00}, {0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x01, 0x02, 0x40, 0x10, 0x08, 0x00, 0x00}, {0x01, 0x02, 0x40, 0x04, 0x08, 0x00, 0x00}, {0x20, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00}, {0x01, 0x20, 0x40, 0x04, 0x08, 0x00, 0x00}, {0x01, 0x20, 0x10, 0x08, 0x04, 0x40, 0x00}, {0x01, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00}, {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40}, {0x01, 0x02, 0x04, 0x08, 0x20, 0x40, 0x00}, }; sbit mode_flag at myFlags.B0; sbit count_down_flag at myFlags.B1; #define SET_MODE (mode_flag == 1) #define RUN_MODE (mode_flag == 0) //Timer1 //Prescaler 1:1; TMR1 Preload = 61536; Actual Interrupt Time : 2 ms //Place/Copy this part in declaration section void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xF0; TMR1L = 0x60; TMR1IE_bit = 1; INTCON = 0xC0; } void Interrupt() { if(TMR1IF_bit) { //Enter your code here SSD_DATA_PORT = 0x00; PORTE = 0x07; switch(segment_index) { case 0: SSD_DATA_PORT = cc_segment_mask[digits[j]][0]; break; case 1: SSD_DATA_PORT = cc_segment_mask[digits[j]][1]; break; case 2: SSD_DATA_PORT = cc_segment_mask[digits[j]][2]; break; case 3: SSD_DATA_PORT = cc_segment_mask[digits[j]][3]; break; case 4: SSD_DATA_PORT = cc_segment_mask[digits[j]][4]; break; case 5: SSD_DATA_PORT = cc_segment_mask[digits[j]][5]; break; case 6: SSD_DATA_PORT = cc_segment_mask[digits[j]][6]; break; }; if((j == 0) && (digits[0] != 0)) { PORTE.F0 = 0; } else if(j == 1) { PORTE.F1 = 0; } else if(j > 1)j = 0; if(++segment_index == 7) { segment_index = 0; if(++j > 1)j = 0; } if(count_down_flag) { if(++two_x_milli_second_counter == 500) { if(--delay_value_in_seconds == 0) { RELAY = ON; count_down_flag = CLEAR; } two_x_milli_second_counter = 0; } } TMR1IF_bit = 0; TMR1H = 0xF0; TMR1L = 0x60; } } void main() { CMCON = 0x07; ADCON1 = 0x87; TRISA = 0x00; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x07; Delay_ms(100); InitTimer1(); while(1) { if(SET_OR_RUN_BUTTON_PRESSED) { Delay_ms(30); while(SET_OR_RUN_BUTTON_PRESSED); mode_flag = ~mode_flag; if(RUN_MODE) { if(delay_value_in_seconds > 0) { RELAY = OFF; count_down_flag = 1; } else if(delay_value_in_seconds == 0) { RELAY = ON; } } else if(SET_MODE) { count_down_flag = 0; } } if(SET_MODE) { if(INCREMENT_BUTTON_PRESSED) { Delay_ms(120); if(INCREMENT_BUTTON_PRESSED) { if(++delay_value_in_seconds > 99) { delay_value_in_seconds = 0; } } } else if(DECREMENT_BUTTON_PRESSED) { Delay_ms(120); if(DECREMENT_BUTTON_PRESSED) { if(--delay_value_in_seconds < 0) { delay_value_in_seconds = 99; } } } } if(delay_value_in_seconds != previous_delay_value_in_seconds) { if((delay_value_in_seconds >= 0) && (delay_value_in_seconds < 10)) { digits[0] = 0; digits[1] = delay_value_in_seconds; } else if((delay_value_in_seconds >= 10) && (delay_value_in_seconds < 100)) { digits[0] = delay_value_in_seconds / 10; digits[1] = delay_value_in_seconds % 10; } previous_delay_value_in_seconds = delay_value_in_seconds; } } }
unsigned char cc_segment_mask[10][7]
const unsigned char cc_segment_mask[10][7]
sbit Set_Or_Run_Button at RB0_bit;
sbit Increment_Button at RB1_bit;
sbit Decrement_Button at RB2_bit;
sbit LED at RD6_bit;
sbit RELAY at RD7_bit;
#define SSD_DATA_PORT PORTC
#define ON 1
#define OFF 0
#define SET 1
#define CLEAR 0
#define TRUE 1
#define FALSE 0
#define SET_OR_RUN_BUTTON_PRESSED (Set_Or_Run_Button == 0)
#define INCREMENT_BUTTON_PRESSED (Increment_Button == 0)
#define DECREMENT_BUTTON_PRESSED (Decrement_Button == 0)
unsigned char myFlags = 0, segment_index = 0, j = 0, digits[2] = {0, 0};
signed char delay_value_in_seconds = 0, previous_delay_value_in_seconds = 1, counter = 0;
unsigned int two_x_milli_second_counter = 0;
const code unsigned char cc_segment_mask[10][7] = {
{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00},
{0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x02, 0x40, 0x10, 0x08, 0x00, 0x00},
{0x01, 0x02, 0x40, 0x04, 0x08, 0x00, 0x00},
{0x20, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00},
{0x01, 0x20, 0x40, 0x04, 0x08, 0x00, 0x00},
{0x01, 0x20, 0x10, 0x08, 0x04, 0x40, 0x00},
{0x01, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00},
{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40},
{0x01, 0x02, 0x04, 0x08, 0x20, 0x40, 0x00},
};
sbit mode_flag at myFlags.B0;
sbit count_down_flag at myFlags.B1;
#define SET_MODE (mode_flag == 1)
#define RUN_MODE (mode_flag == 0)
//Timer1
//Prescaler 1:1; TMR1 Preload = 61536; Actual Interrupt Time : 2 ms
//Place/Copy this part in declaration section
void InitTimer1() {
T1CON = 0x01;
TMR1IF_bit = 0;
TMR1H = 0xF0;
TMR1L = 0x60;
TMR1IE_bit = 1;
INTCON = 0xC0;
}
void Interrupt() {
if(TMR1IF_bit) {
//Enter your code here
SSD_DATA_PORT = 0x00;
PORTE = 0x07;
switch(segment_index) {
case 0:
SSD_DATA_PORT = cc_segment_mask[digits[j]][0];
break;
case 1:
SSD_DATA_PORT = cc_segment_mask[digits[j]][1];
break;
case 2:
SSD_DATA_PORT = cc_segment_mask[digits[j]][2];
break;
case 3:
SSD_DATA_PORT = cc_segment_mask[digits[j]][3];
break;
case 4:
SSD_DATA_PORT = cc_segment_mask[digits[j]][4];
break;
case 5:
SSD_DATA_PORT = cc_segment_mask[digits[j]][5];
break;
case 6:
SSD_DATA_PORT = cc_segment_mask[digits[j]][6];
break;
};
if((j == 0) && (digits[0] != 0)) {
PORTE.F0 = 0;
}
else if(j == 1) {
PORTE.F1 = 0;
}
if(++segment_index == 7) {
segment_index = 0;
if(++j > 1)j = 0;
}
if(count_down_flag) {
if(++two_x_milli_second_counter == 500) {
if(--delay_value_in_seconds == 0) {
RELAY = ON;
count_down_flag = CLEAR;
}
two_x_milli_second_counter = 0;
}
}
TMR1IF_bit = 0;
TMR1H = 0xF0;
TMR1L = 0x60;
}
}
void main() {
CMCON = 0x07;
ADCON1 = 0x87;
TRISA = 0x00;
TRISB = 0x07;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
PORTD = 0x00;
PORTE = 0x07;
Delay_ms(100);
InitTimer1();
while(1) {
if(SET_OR_RUN_BUTTON_PRESSED) {
Delay_ms(30);
while(SET_OR_RUN_BUTTON_PRESSED);
mode_flag = ~mode_flag;
if(RUN_MODE) {
if(delay_value_in_seconds > 0) {
RELAY = OFF;
count_down_flag = 1;
}
else if(delay_value_in_seconds == 0) {
RELAY = ON;
}
}
else if(SET_MODE) {
count_down_flag = 0;
}
}
if(SET_MODE) {
if(INCREMENT_BUTTON_PRESSED) {
Delay_ms(120);
if(INCREMENT_BUTTON_PRESSED) {
if(++delay_value_in_seconds > 99) {
delay_value_in_seconds = 0;
}
}
}
else if(DECREMENT_BUTTON_PRESSED) {
Delay_ms(120);
if(DECREMENT_BUTTON_PRESSED) {
if(--delay_value_in_seconds < 0) {
delay_value_in_seconds = 99;
}
}
}
}
if(delay_value_in_seconds != previous_delay_value_in_seconds) {
if((delay_value_in_seconds >= 0) && (delay_value_in_seconds < 10)) {
digits[0] = 0;
digits[1] = delay_value_in_seconds;
}
else if((delay_value_in_seconds >= 10) && (delay_value_in_seconds < 100)) {
digits[0] = delay_value_in_seconds / 10;
digits[1] = delay_value_in_seconds % 10;
}
previous_delay_value_in_seconds = delay_value_in_seconds;
}
}
}
which book is the best refrence for learning mikroc pic16?(a book that have a lot of example)Change
toCode:unsigned char cc_segment_mask[10][7]
It will save RAM.Code:const unsigned char cc_segment_mask[10][7]
Here is the modified code and modified project. Project attached.
Code:sbit Set_Or_Run_Button at RB0_bit; sbit Increment_Button at RB1_bit; sbit Decrement_Button at RB2_bit; sbit LED at RD6_bit; sbit RELAY at RD7_bit; #define SSD_DATA_PORT PORTC #define ON 1 #define OFF 0 #define SET 1 #define CLEAR 0 #define TRUE 1 #define FALSE 0 #define SET_OR_RUN_BUTTON_PRESSED (Set_Or_Run_Button == 0) #define INCREMENT_BUTTON_PRESSED (Increment_Button == 0) #define DECREMENT_BUTTON_PRESSED (Decrement_Button == 0) unsigned char myFlags = 0, segment_index = 0, j = 0, digits[2] = {0, 0}; signed char delay_value_in_seconds = 0, previous_delay_value_in_seconds = 1, counter = 0; unsigned int two_x_milli_second_counter = 0; const code unsigned char cc_segment_mask[10][7] = { {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00}, {0x02, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x01, 0x02, 0x40, 0x10, 0x08, 0x00, 0x00}, {0x01, 0x02, 0x40, 0x04, 0x08, 0x00, 0x00}, {0x20, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00}, {0x01, 0x20, 0x40, 0x04, 0x08, 0x00, 0x00}, {0x01, 0x20, 0x10, 0x08, 0x04, 0x40, 0x00}, {0x01, 0x02, 0x04, 0x00, 0x00, 0x00, 0x00}, {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40}, {0x01, 0x02, 0x04, 0x08, 0x20, 0x40, 0x00}, }; sbit mode_flag at myFlags.B0; sbit count_down_flag at myFlags.B1; #define SET_MODE (mode_flag == 1) #define RUN_MODE (mode_flag == 0) //Timer1 //Prescaler 1:1; TMR1 Preload = 61536; Actual Interrupt Time : 2 ms //Place/Copy this part in declaration section void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xF0; TMR1L = 0x60; TMR1IE_bit = 1; INTCON = 0xC0; } void Interrupt() { if(TMR1IF_bit) { //Enter your code here SSD_DATA_PORT = 0x00; PORTE = 0x07; switch(segment_index) { case 0: SSD_DATA_PORT = cc_segment_mask[digits[j]][0]; break; case 1: SSD_DATA_PORT = cc_segment_mask[digits[j]][1]; break; case 2: SSD_DATA_PORT = cc_segment_mask[digits[j]][2]; break; case 3: SSD_DATA_PORT = cc_segment_mask[digits[j]][3]; break; case 4: SSD_DATA_PORT = cc_segment_mask[digits[j]][4]; break; case 5: SSD_DATA_PORT = cc_segment_mask[digits[j]][5]; break; case 6: SSD_DATA_PORT = cc_segment_mask[digits[j]][6]; break; }; if((j == 0) && (digits[0] != 0)) { PORTE.F0 = 0; } else if(j == 1) { PORTE.F1 = 0; } if(++segment_index == 7) { segment_index = 0; if(++j > 1)j = 0; } if(count_down_flag) { if(++two_x_milli_second_counter == 500) { if(--delay_value_in_seconds == 0) { RELAY = ON; count_down_flag = CLEAR; } two_x_milli_second_counter = 0; } } TMR1IF_bit = 0; TMR1H = 0xF0; TMR1L = 0x60; } } void main() { CMCON = 0x07; ADCON1 = 0x87; TRISA = 0x00; TRISB = 0x07; TRISC = 0x00; TRISD = 0x00; TRISE = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; PORTE = 0x07; Delay_ms(100); InitTimer1(); while(1) { if(SET_OR_RUN_BUTTON_PRESSED) { Delay_ms(30); while(SET_OR_RUN_BUTTON_PRESSED); mode_flag = ~mode_flag; if(RUN_MODE) { if(delay_value_in_seconds > 0) { RELAY = OFF; count_down_flag = 1; } else if(delay_value_in_seconds == 0) { RELAY = ON; } } else if(SET_MODE) { count_down_flag = 0; } } if(SET_MODE) { if(INCREMENT_BUTTON_PRESSED) { Delay_ms(120); if(INCREMENT_BUTTON_PRESSED) { if(++delay_value_in_seconds > 99) { delay_value_in_seconds = 0; } } } else if(DECREMENT_BUTTON_PRESSED) { Delay_ms(120); if(DECREMENT_BUTTON_PRESSED) { if(--delay_value_in_seconds < 0) { delay_value_in_seconds = 99; } } } } if(delay_value_in_seconds != previous_delay_value_in_seconds) { if((delay_value_in_seconds >= 0) && (delay_value_in_seconds < 10)) { digits[0] = 0; digits[1] = delay_value_in_seconds; } else if((delay_value_in_seconds >= 10) && (delay_value_in_seconds < 100)) { digits[0] = delay_value_in_seconds / 10; digits[1] = delay_value_in_seconds % 10; } previous_delay_value_in_seconds = delay_value_in_seconds; } } }
Read this free book.which book is the best refrence for learning mikroc pic16?(a book that have a lot of example)
thanksRead this free book.
http://learn.mikroe.com/ebooks/piccprogramming/
For Timer Calculation you can study this.
http://www.best-microcontroller-projects.com/article-pic-timer-calculation.html
You can use this tool to generate Timer delays and its mikroC PRO Code.
http://www.mikroe.com/timer-calculator/
But it is free to read.but this book is not free for download
by Jake Hertz
by Aaron Carman
by Jake Hertz
by Aaron Carman