richardlaishram
Member level 4
- Joined
- Jan 6, 2013
- Messages
- 77
- Helped
- 6
- Reputation
- 12
- Reaction score
- 6
- Trophy points
- 1,298
- Location
- Planet Earth
- Activity points
- 1,804
#include <htc.h>
#define Count_In RB3
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF);
void main(void)
{
Button = 0;
TRISB3 = 1;
unsigned int Count = 0;
while(1)
{
if(Button == 1) // Button is pressed
{
while(1)
{
while(Button == 1); // Wait for de-assertion of the button
__delay_ms(10);
Count++;
if(Count ==3)
Count = 1;
switch(Count)
{
case 1:
{
WriteStringToLCD("Loop 1"); // Write Message on LCD
__delay_ms(50);
}break;
case 2:
{
WriteStringToLCD("Loop 1"); // Write Message on LCD
__delay_ms(50);
}break;
}
}
}
}
Code C - [expand] 1 if(Button == 1)
Code C - [expand] 1 if(Count_In == 1)
View attachment 96328
This is the Proteus Simulation and here it's auto incrementing the count and looping through the various cases when I put the break; inside the loop. When I flashed it to the microcontroller it's the same and not looping as expected.
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 #include <htc.h> #define Count_In RB3 __CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF); void main(void) { Button = 0; TRISB3 = 1; unsigned int Count = 0; while(1) { if(Button == 1) // Button is pressed { while(1) { while(Button == 0); // extra added while(Button == 1); // Wait for de-assertion of the button __delay_ms(10); Count++; if(Count ==3) Count = 1; switch(Count) { case 1: { WriteStringToLCD("Loop 1"); // Write Message on LCD __delay_ms(50); }break; case 2: { WriteStringToLCD("Loop 2"); // Write Message on LCD // you can not find the diff if it was the same statement __delay_ms(50); }break; } } } }
#define Count_In RB3
Button = 0; // Initialize RB3 as 0
TRISB3 = 1; // RB3 as Input
unsigned int Count = 0;
Code:#define Count_In RB3 Button = 0; // Initialize RB3 as 0 TRISB3 = 1; // RB3 as Input unsigned int Count = 0;
Even if it's inside the inner while loop, it will still check if the button is pressed or not as far as I know. Please let me know if I have done anything wrong while initializing the pin.
Code C - [expand] 1 2 #define Button RB3 TRISB3 = 1;
What is exactly your problem???
#include <htc.h>
#include "LCD.h"
#define Count_In RB2
__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF);
void main(void)
{
InitLCD();
TRISB2 = 1;
TRISC = 0;
unsigned int Count = 0;
WriteStringToLCD("Initializing...");
while(1)
{
if(Count_In == 1) // If Count button is pressed
{
while(Count_In == 0)
{
while(Count_In == 0);
if(Count_In == 1); // Wait for de-assertion of the button
__delay_ms(10);
Count++; // Increment Count value
if(Count == 3)
Count = 1; // Reset Count
switch (Count)
{
case 1:
{
ClearLCDScreen();
WriteStringToLCD("Mode 1 ON"); // Write Message on LCD
__delay_ms(100);
WriteStringToLCD("Mode 1 OFF");
__delay_ms(100);
break;
}
case 2:
{
ClearLCDScreen();
WriteStringToLCD("Mode 2 ON"); // Write Message on LCD
__delay_ms(100);
WriteStringToLCD("Mode 2 OFF");
__delay_ms(100);
break;
}
}
}
}
}
}
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 #include <htc.h> #include "LCD.h" #define Count_In RB2 __CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF); bit old_count; void main(void) { InitLCD(); TRISB2 = 1; TRISC = 0; unsigned int Count = 0; WriteStringToLCD("Initializing..."); while(1) { if(Count_In == 1 && old_count != Count_In); Count = (count == 1)? 2 : 1; old_count = Count_In; switch (Count) { case 1: { ClearLCDScreen(); WriteStringToLCD("Mode 1 ON"); // Write Message on LCD __delay_ms(1000); WriteStringToLCD("Mode 1 OFF"); __delay_ms(1000); break; } case 2: { ClearLCDScreen(); WriteStringToLCD("Mode 2 ON"); // Write Message on LCD __delay_ms(1000); WriteStringToLCD("Mode 2 OFF"); __delay_ms(1000); break; } } } }
Code C - [expand] 1 if(Count_In == 1 && old_count != Count_In);
you know what is the problem ??
it is the semicolon""
Code C - [expand] 1 if((Count_In == 1) && (old_count != Count_In));
Thanks a ton. Now the only problem is that instead of pressing the button once to start the modes, it's starting automatically. I'll have to work on it to figure that out.may be its the problem of thread handling so i have updated a completely new version with more quick response use the project file as it was....
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?