Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 #define BRAKE_FREQUENCY_MIN 2110 #define BRAKE_FREQUENCY_MAX 2130 #define RH_FREQUENCY_MIN 1400 #define RH_FREQUENCY_MAX 1420 #define LH_FREQUENCY_MIN 990 #define LH_FREQUENCY_MAX 1010 #define BRAKE_RH_FREQUENCY_MIN 840 #define BRAKE_RH_FREQUENCY_MAX 860 #define BRAKE_LH_FREQUENCY_MIN 670 #define BRAKE_LH_FREQUENCY_MAX 690 #define OFF 0 #define ON 1 #define HUNDRED_MILLI_SECONDS_TIMER_TICKS_VALUE 10 sbit Red_led at LATA0_bit; sbit Green_led at LATA1_bit; sbit Blue_led at LATA2_bit; sbit Test_Signal at LATA4_bit; unsigned char my_flags = 0; unsigned timer_ticks = 0; unsigned int frequency = 0; sbit reading_complete_flag at my_flags.B0; //Timer2 //Prescaler 1:16; Postscaler 1:5; TMR2 Preload = 250; Actual Interrupt Time : 10.0025 ms //Place/Copy this part in declaration section void InitTimer2() { T2CON = 0x26; PR2 = 250; TMR2IE_bit = 1; } void Interrupt() { if(TMR1IF_bit) { ++frequency; //not required as frequency read is less than 3KHz TMR1IF_bit = 0; } if((TMR2IE_bit) && (TMR2IF_bit)) { //Enter your code here if(++timer_ticks == HUNDRED_MILLI_SECONDS_TIMER_TICKS_VALUE) { TMR1ON_bit = 0; TMR2IE_bit = 0; timer_ticks = 0; Test_Signal = ~Test_Signal; reading_complete_flag = 1; } TMR2IF_bit = 0; } } void Start_Reading_Input_Signal() { Test_Signal = 0; T1GCON = 0x00; InitTimer2(); TMR1H = 0; TMR1L = 0; TMR1IE_bit = 1; T1CON = 0x85; PEIE_bit = 1; GIE_bit = 1; } void main() { OSCCON = 0x73; OSCSTAT = 0x99; OSCTUNE = 0x00; asm clrwdt OPTION_REG = 0x8F; CM1CON0 = 0x00; ANSELA = 0x00; TRISA = 0x20; PORTA = 0x00; LATA = 0x00; Delay_ms(100); reading_complete_flag = 0; Start_Reading_Input_Signal(); while(1) { asm clrwdt if(reading_complete_flag) { //frequency = ((frequency << 16) + (TMR1H << 8) + TMR1L) * 10;// frequency = ((TMR1H << 8) + TMR1L) * 10; if((frequency > BRAKE_FREQUENCY_MIN) && (frequency < BRAKE_FREQUENCY_MAX)) { Red_Led = ON; Green_Led = OFF; Blue_Led = OFF; } else if((frequency > RH_FREQUENCY_MIN) && (frequency < RH_FREQUENCY_MAX)) { Red_Led = OFF; Green_Led = ON; Blue_Led = OFF; } else if((frequency > LH_FREQUENCY_MIN) && (frequency < LH_FREQUENCY_MAX)) { Red_Led = OFF; Green_Led = OFF; Blue_Led = ON; } else if((frequency > BRAKE_RH_FREQUENCY_MIN) && (frequency < BRAKE_RH_FREQUENCY_MAX)) { Red_Led = ON; Green_Led = ON; Blue_Led = OFF; } else if((frequency > BRAKE_LH_FREQUENCY_MIN) && (frequency < BRAKE_LH_FREQUENCY_MAX)) { Red_Led = ON; Green_Led = OFF; Blue_Led = ON; } frequency = 0; reading_complete_flag = 0; Start_Reading_Input_Signal(); } } }
I see 10ms Timer2 Interrupt working by toggling a pin but Leds doesn't turn ON.
it seems you range of frequency to measure is from 670 to 2130 hz ?
// remove comment for test leds ,instead Freq meausre
// #define Test_Leds //
#define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row
#define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row
#define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row
#define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row
#define _LCD_CLEAR 0x01 //Clear display
#define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a shifted display to
//its original position. Display data RAM is unaffected.
#define _LCD_CURSOR_OFF 0x0C //Turn off cursor
#define _LCD_UNDERLINE_ON 0x0E //Underline cursor on
#define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on
#define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing display data RAM
#define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing display data RAM
#define _LCD_TURN_ON 0x0C //Turn Lcd display on
#define _LCD_TURN_OFF 0x08 //Turn Lcd display off
#define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing display data RAM
#define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing display data RAM
#define LCD_ADDR 0x4E
#define BRAKE_FREQUENCY_MIN 2110
#define BRAKE_FREQUENCY_MAX 2130
#define RH_FREQUENCY_MIN 1400
#define RH_FREQUENCY_MAX 1420
#define LH_FREQUENCY_MIN 990
#define LH_FREQUENCY_MAX 1010
#define BRAKE_RH_FREQUENCY_MIN 840
#define BRAKE_RH_FREQUENCY_MAX 860
#define BRAKE_LH_FREQUENCY_MIN 670
#define BRAKE_LH_FREQUENCY_MAX 690
#define OFF 0
#define ON 1
#define TEN_TIMES_TIMER2_TICKS_VALUE 10
/*
HARDWARE
Pin 1 +VCC 5V
Pin 2 RA5 ---T1G Timer1
Pin 3
Pin 4 RA2
Pin 4 RA3 VPP --390----- VPP ICSP
Pin 5 RA2 ----- 390 --- Led --
Pin 5 RA2 ----- I2C SDA ------- LCD
Pin 6 RA1 ----- I2C SCL ------ LCD
Pin 6 RA1 ----- 390 --- Led --
Pin 6 RA1 ------ jaune ---- ICSP Data
Pin 7 RA0 ----- 390 --- Led --
Pin 7 RA0 ------- vert ----- ICSP Clock
Pin 8 0V Gnd --------ICSP Gnd
*/
sbit Red_Led at LATA0_bit;
sbit Green_Led at LATA4_bit;
sbit Blue_Led at LATA2_bit;
sbit Test_Signal at PORTA.B3;
sbit Red_Led_Direction at TRISA.B0;
sbit Green_Led_Direction at TRISA.B4;
sbit Blue_Led_Direction at TRISA.B2;
sbit Test_Signal_Direction at TRISA.B3;
const unsigned char Efface[]=" "; // 20 spaces
char txt1[] = "Frequency:";
char txt[21];
unsigned char CRam1[21];
unsigned int i,j,k;
unsigned char my_flags = 0;
volatile unsigned timer_ticks = 0;
unsigned int frequency = 0;
long Timer1_Overflow;
unsigned previous_frequency = 3000;
volatile int Time_Elapsed;
unsigned int Test_Leds_Values[]={2120,1410,1000,680};
sbit reading_complete_flag at my_flags.B0;
void Interrupts() iv 0x0004 ics ICS_AUTO
{
if((TMR1IE_bit)&& (TMR1IF_bit))
{
Timer1_Overflow=Timer1_Overflow+65536;
// ++frequency; //not required as frequency read is less than 3KHz
TMR1IF_bit = 0;
}
if((TMR2IE_bit) && (TMR2IF_bit) )
{
//Enter your code here
if(++timer_ticks >= TEN_TIMES_TIMER2_TICKS_VALUE )
{
TMR1ON_bit = 0;
TMR1IF_bit = 0;
TMR2IE_bit = 0;
TMR1IE_bit=0;
timer_ticks = 0;
//Test_Signal = ~Test_Signal;
//reading_complete_flag = 1;
Time_Elapsed=1;
GIE_bit=0;
}
TMR2IF_bit = 0;
}
}
//Timer2
//Prescaler 1:16; Postscaler 1:5; TMR2 Preload = 250; Actual Interrupt Time : 10.0025 ms
//Place/Copy this part in declaration section
void InitTimer2() {
T2CON = 0x26;
PR2 = 250;
timer_ticks=0;
TMR2IF_bit=0;
TMR2IE_bit = 1;
}
void Start_Reading_Input_Signal()
{
T1CON=0;
// Test_Signal = 0;
T1GCON=0; //T1GSS<1:0>: Timer1 Gate Source Select bits 00 = Timer1 Gate pin
Timer1_Overflow=0;
TMR1H = 0;
TMR1L = 0;
Time_Elapsed=0;
//bit 7-6 10 =Timer1 clock source is pin or oscillator:
// bit 5-4 T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits 00 => 1:1 Prescale value
// bit 3 T1OSCEN: LP Oscillator Enable Control bit 0 = Dedicated Timer1 oscillator circuit disabled
//bit 2 T1SYNC: Timer1 External Clock Input TMR1CS<1:0> = 1X
//1 0 0 External Clocking on T1CKI Pin
// bit 8=1 and b2=1 = Do not synchronize external clock input
//bit 0 TMR1ON: Timer1 On bit1 1= Enables Timer1
T1CON = 0x85;
InitTimer2();
TMR1IE_bit = 1;
PEIE_bit = 1;
GIE_bit = 1;
}
void I2C_LCD_Cmd(char out_char) {
char hi_n, lo_n;
char rs = 0x00;
hi_n = out_char & 0xF0;
lo_n = (out_char << 4) & 0xF0;
I2C1_Start();
I2C1_Is_Idle();
I2C1_Wr(LCD_ADDR);
I2C1_Is_Idle();
I2C1_Wr(hi_n | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(hi_n | rs | 0x00 | 0x08);
I2C1_Is_Idle();
Delay_us(100);
I2C1_Wr(lo_n | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(lo_n | rs | 0x00 | 0x08);
I2C1_Is_Idle();
I2C1_Stop();
if(out_char == 0x01)Delay_ms(2);
}
void I2C_LCD_Chr(char row, char column, char out_char) {
char hi_n, lo_n;
char rs = 0x01;
switch(row){
case 1:
I2C_LCD_Cmd(0x80 + (column - 1));
break;
case 2:
I2C_LCD_Cmd(0xC0 + (column - 1));
break;
case 3:
I2C_LCD_Cmd(0x94 + (column - 1));
break;
case 4:
I2C_LCD_Cmd(0xD4 + (column - 1));
break;
};
hi_n = out_char & 0xF0;
lo_n = (out_char << 4) & 0xF0;
I2C1_Start();
I2C1_Is_Idle();
I2C1_Wr(LCD_ADDR);
I2C1_Is_Idle();
I2C1_Wr(hi_n | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(hi_n | rs | 0x00 | 0x08);
I2C1_Is_Idle();
Delay_us(100);
I2C1_Wr(lo_n | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(lo_n | rs | 0x00 | 0x08);
I2C1_Is_Idle();
I2C1_Stop();
}
void I2C_LCD_Chr_Cp(char out_char) {
char hi_n, lo_n;
char rs = 0x01;
hi_n = out_char & 0xF0;
lo_n = (out_char << 4) & 0xF0;
I2C1_Start();
I2C1_Is_Idle();
I2C1_Wr(LCD_ADDR);
I2C1_Is_Idle();
I2C1_Wr(hi_n | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(hi_n | rs | 0x00 | 0x08);
I2C1_Is_Idle();
Delay_us(100);
I2C1_Wr(lo_n | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(lo_n | rs | 0x00 | 0x08);
I2C1_Is_Idle();
I2C1_Stop();
}
void I2C_LCD_Init() {
char rs = 0x00;
I2C1_Start();
I2C1_Is_Idle();
I2C1_Wr(LCD_ADDR);
I2C1_Is_Idle();
Delay_ms(100);
I2C1_Wr(0x30 | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(0x30 | rs | 0x00 | 0x08);
I2C1_Is_Idle();
Delay_ms(10);
I2C1_Wr(0x30 | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(0x30 | rs | 0x00 | 0x08);
I2C1_Is_Idle();
Delay_ms(10);
I2C1_Wr(0x30 | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(0x30 | rs | 0x00 | 0x08);
I2C1_Is_Idle();
Delay_ms(10);
I2C1_Wr(0x20 | rs | 0x04 | 0x08);
I2C1_Is_Idle();
Delay_us(50);
I2C1_Wr(0x20 | rs | 0x00 | 0x08);
I2C1_Is_Idle();
I2C1_Stop();
Delay_ms(10);
I2C_LCD_Cmd(0x28);
I2C_LCD_Cmd(0x06);
}
void I2C_LCD_Out(char row, char col, char *text) {
while(*text) {
I2C_LCD_Chr(row, col++, *text++);
}
}
void I2C_LCD_Out_Cp(char *text) {
while(*text) {
I2C_LCD_Chr_Cp(*text++);
}
}
void I2C_LCD__CLS()
{
I2C_LCD_Cmd(0x80);
I2C_LCD_Out(1,1,(const char *) Efface );
I2C_LCD_Cmd(0x80);
}
void StrConstRamCopy(char *dest, const char *source) {
while(*source) *dest++ = *source++ ;
*dest = 0 ; // terminateur
}
void main() {
OSCCON = 0x70; // No PLL 8MHz
// OSCSTAT = 0x99; // read only !
OSCTUNE = 0x00;
// WUPEN =1 Pull up disable PSA=1 Prescaler NOT for Timer0 Prescaler 1/256
// OPTION_REG = 0x8F;
CM1CON0=0; // disable comparators
CM1CON1=0;
PORTA = 0x00;
ANSELA = 0x00;
TRISA = 0b00101000; // RA4 output , RA5 as TMR1 input ,RA3 = Pin MCLR used as input
Red_Led_Direction=0;//RA0;
Green_Led_Direction=0;//RA4
Blue_Led_Direction=0; //RA2;
Test_Signal_Direction=1;// RA3=MCLR
Green_Led=0;
Blue_Led=0;
Red_Led=0;
Delay_ms(2000);
I2C1_Init(100000);
I2C_LCD_Init();
I2C_LCD_Cmd(_LCD_CURSOR_OFF);
I2C_LCD_Cmd(_LCD_CLEAR);
I2C_LCD__CLS();
Delay_ms(1000);
I2C_LCD_Out(1,1,txt1);
Delay_ms(1000);
i=0;
// reading_complete_flag = 0;
//12345678901234567890
#ifdef Test_Leds
I2C_LCD_Out(4,1,"Zone Test Led: ");
#else
I2C_LCD_Out(4,1,"Indice : ");
#endif
while(1)
{
#ifdef Test_Leds
for (j=0;j<4;j++)
{
frequency=Test_Leds_Values[j];
#endif
Start_Reading_Input_Signal();
while(Time_Elapsed==0);
#ifdef Test_Leds
WordToStr(j+1,CRam1);
I2C_LCD_Out(4,14,CRam1);
#else
WordToStr(i,CRam1);
I2C_LCD_Out(4,10,CRam1);
#endif
i++;
if(Timer1_Overflow>0)
{
//frequency = ((frequency << 16) + (TMR1H << 8) + TMR1L) * 10; <- integer over range !!//
I2C_LCD_Out(2,1,"Over Range Timer1");
Timer1_Overflow=0;
}
else
{
frequency = ( (TMR1H << 8) + TMR1L) * 10;
WordToStr(frequency,CRam1);
//12345678901234567890
StrConstRamCopy(txt,"Frequency : Hz");
memcpy(txt+12,CRam1,5);
I2C_LCD_Out(1,1,txt);
}
#ifndef Test_Leds
if(previous_frequency != frequency)
{
#endif
// BRAKE_FREQUENCY_MIN 2110 BRAKE_FREQUENCY_MAX 2130
if((frequency > BRAKE_FREQUENCY_MIN) && (frequency < BRAKE_FREQUENCY_MAX)) {
Red_Led = ON;
Green_Led = OFF;
Blue_Led = OFF;
}
//RH_FREQUENCY_MIN 1400 RH_FREQUENCY_MAX 1420
else if((frequency > RH_FREQUENCY_MIN) && (frequency < RH_FREQUENCY_MAX)) {
Red_Led = OFF;
Green_Led = ON;
Blue_Led = OFF;
}
// LH_FREQUENCY_MIN 990 LH_FREQUENCY_MAX 1010
else if((frequency > LH_FREQUENCY_MIN) && (frequency < LH_FREQUENCY_MAX)) {
Red_Led = OFF;
Green_Led = OFF;
Blue_Led = ON;
}
//BRAKE_RH_FREQUENCY_MIN 840 BRAKE_RH_FREQUENCY_MAX 860
else if((frequency > BRAKE_RH_FREQUENCY_MIN) && (frequency < BRAKE_RH_FREQUENCY_MAX)) {
Red_Led = ON;
Green_Led = ON;
Blue_Led = OFF;
}
// BRAKE_LH_FREQUENCY_MIN 670 BRAKE_LH_FREQUENCY_MAX 690
else if((frequency > BRAKE_LH_FREQUENCY_MIN) && (frequency < BRAKE_LH_FREQUENCY_MAX)) {
Red_Led = ON;
Green_Led = OFF;
Blue_Led = ON;
}
previous_frequency = frequency;
}
//frequency = 0; // ???
#ifdef Test_Leds
Delay_ms(8000); // enough time to see LEDs changes
}
} // for (i
#else
reading_complete_flag = 0;
Start_Reading_Input_Signal();
}
#endif
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?