Change the screen of LCD using button - c

I have two buttons first mode button and second set button connected to the 8051 microcontroller. The mode button changes the LCD screen and the set button blinks the cursor when the individual button is pressed. I am just toggling the mode button so that when the cursor is blinked the mode button performs the function of changing the screen and when the cursor is not blinked it preforms another function. But when the cursor is not blinked it's performing the function but when the cursor is blinked it changes the screen of the LCD but returns back to the initial state without pressing the button again. I want it when the cursor is blinked and the mode button is pressed to change the screen of the LCD and after reaching the last screen return to the initial first screen.
typedef int bool;
#define true 1
#define false 0
bool blink_enabled = false;
unsigned char setmode_in = 0;
static unsigned count = 0;
void display_lcd(void){
switch(key_code){
case KEY_UP:
if (setmode_in == 0){
lcd_out(0x01); //clear the screen of LCD
}else{
count = (count+1)%3;
setmode_in=0;
switch(count){
case 0:
change_screen1();
break;
case 1:
change_screen2();
break;
case 2:
change_screen3();
break;
default: break;
}
case KEY_SET:
if(blink_enabled == true){
lcd_out(0x0c); // cursor off
blink_enabled = false;
setmode_in = 0;
} else{
lcd_out(0x0F); //blink the cursor
blink_enabled = true;
setmode_in = 1;
}
break;
default: break;
}
key_code = 0;
}

Related

How to return back when the button is clicked?

When I first press the button, it performs the first function, on the second press, it performs the second task when the third press I want to return and perform the same first function.
void blinkcursor(void);
int count = 0;
void blinkcursor(void)
{
label:
if (button == 1) { //button pressed
if (count == 0) {
count++;
set_cursor_position (1, 2);
lcd_cout(0x0f); //cursor blink
button = 0;
}
else if(count == 1) {
lcd_cout(0x0c); //blink off
button = 0;
}
goto label;
}
}
You didn't say much about the overall structure of your program, so I am just going to assume that you have some main loop running that regularly checks the state of the button, performs any necessary debouncing, and sets button to 1 whenever it detects that the button was pressed. I will also assume that the cursor is not blinking by default, and that the button is only used to toggle the blinking of the cursor.
Now just add a call to the following function in your main loop:
bool blink_enabled = false;
void update_blink(void)
{
if (button) {
// Button was pressed.
if (blink_enabled) {
// Turn off blinking.
lcd_cout(0x0c);
blink_enabled = false;
}
else {
// Turn on blinking.
set_cursor_position(1, 2);
lcd_cout(0x0f);
blink_enabled = true;
}
button = 0;
}
}
(You might need to add #include <stdbool.h> at the top to get the bool type.)

how to write mouse program in linux

I have tried to write a program which will detect the mouse movement. But it is showing error while running in linux environment. i also want to implement the program where if user move the mouse in x axis for different distance like 2cm, 4 cm then it will print some statement. how can i initialize the mouse in linux and get the cursur point coordinate(x,y) while user move mouse.
#include <dos.h>
#include <graphics.h>
union REGS in, out;
void detect_mouse ()
{
in.x.ax = 0;
int86 (0X33,&in,&out); //invoke interrupt
if (out.x.ax == 0)
printf ("\nMouse Failed To Initialize");
else
printf ("\nMouse was Succesfully Initialized");
}
void showmouse_graphics ()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
in.x.ax = 1;
int86 (0X33,&in,&out);
getch ();
closegraph ();
}
void detect ()
{
int button;
while (!kbhit () )
{
in.x.ax = 3;
int86 (0X33,&in,&out);
button=out.x.bx&7
switch(button)
{
case 1:
print(“left button pressed\n”);
break;
case 2:
print(“right button pressed\n”);
break;
case 4:
print(“middle button pressed\n”);
break;
case 3:
print(“left and right button pressed\n”);
break;
case 5:
print(“left and middle button pressed\n”);
break;
case 6:
print(“right and middle button pressed\n”);
break;
case 7:
print(“all the three buttons pressed\n”);
break;
default:
print(“No button pressed\n”);
}
delay (200); // Otherwise due to quick computer response 100s of words will get print
}
}
void hide_mouse ()
{
in.x.ax = 2;
int86 (0X33,&in,&out);
}
int main ()
{
detect_mouse ();
showmouse_graphics ();
detect ();
hide_mouse ();
return 0;
}
You are using dos.h which is a header available for MS/PC-DOS, not available in Linux. It's my strong belief that it doesn't even compile under gcc in Linux.

stm32F4 7-segment display

I have a problem with programming quad 7-segment display. I don't know how to make all multiplexed chars blinking.
I'm programming in CooCox
multiplexing code (interrupt):
void TIM2_IRQHandler(){
if (TIM_GetITStatus(TIM2,TIM_IT_Update)) {
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
GPIO_ResetBits(GPIOC,15); //turn off all display cells
switch (disp) {
case 1:
decodeCharacters(digits[(alarm.RTC_AlarmTime.RTC_Minutes-time.RTC_Minutes)%10]); //called method decoding chars
break;
case 2:
decodeCharacters(digits[(alarm.RTC_AlarmTime.RTC_Seconds-time.RTC_Seconds)/10]);
break;
case 3:
decodeCharacters(digits[(alarm.RTC_AlarmTime.RTC_Seconds-time.RTC_Seconds)%10]);
break;
default:
decodeCharacters(digits[(alarm.RTC_AlarmTime.RTC_Minutes-time.RTC_Minutes)/10]);
break;
}
GPIO_ToggleBits(GPIOC,1<<disp); //turn on display cell
disp = (disp+1)%4;
}
}
where "disp" is unsigned integer.
I understand that you have a code that displays time and you want to make your digits blinking.
First thing that you need to do is to check how often your interrupt handler occurs. Then inside this handler you can create a static variable which will count time, e.g.
static unsigned int blinkCounter = 0;
if( blinkCounter < 500 )
{
/* Turn off the display */
}
else
{
/* Most of your current handler code */
}
if( blinkCounter > 1000 )
{
blinkCounter = 0;
}
blinkCounter++;

Why this code is skipping points while moving the mouse fast?

I started learning the SDL library yesterday and, after some reading and asking, made a very simple program that draws a block when the left button is down.
The problem is that it skips points when the mouse is moved fast, so you get a bunch of squares instead of a line, the following screenshot shows one line made moving the mouse with regular speed and one while moving it fast:
What is causing it to skip all those points?
Here's the code:
//keep the window open
while(running){
//handle events
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_MOUSEBUTTONDOWN:
//left button down draws black block
if(event.button.button == SDL_BUTTON_LEFT) boxColor = black;
//right button "erases" a point
else
if(event.button.button == SDL_BUTTON_RIGHT) boxColor = blue;
//middle button clears the screen
else {
clearScreen(display,blue);
break;
}
//where to draw
drawing = 1;
boxRect.x = event.button.x - BOX_WIDTH / 2;
boxRect.y = event.button.y - BOX_HEIGHT / 2;
break;
case SDL_MOUSEMOTION:
//keep drawing if the button is pressed
if(drawing == 1){
boxRect.x = event.motion.x - BOX_WIDTH / 2;
boxRect.y = event.motion.y - BOX_HEIGHT / 2;
}
break;
//stop drawing when the button is no longer pressed
case SDL_MOUSEBUTTONUP:
drawing = 0;
break;
//quit if window closing button is pressed
case SDL_QUIT:
running = 0;
break;
}
}
//draw
if(drawing == 1){
SDL_FillRect(display,&boxRect,boxColor);
SDL_Flip(display);
}
}
Because the system doesn't actually get the points as a continuous stream, it has to poll for the mouse position. This means that if you move the mouse too fast, the difference between two polls will be big enough for there to be a gap.

Set a switch on until a flag is reached and then set the switch to off

What would be the best and easiest(simplest) way to do this..?
I am writing code in C or Capel (Vector CANoe)
I have a switch on a panel which I have designed in Vector CANoe , turns a motor on and off (0 == off and 1 == on) and I want to turn the motor off when it reaches its limit .. to stop the motor burning out ! (we know it has reached its limit when the a sensor says that the limit is reached (sensor = 1 for limit reached and 0 for not reached)
How would I do this?
i want to do something like this
WHILE (clutchMotorPower==1 & sensorClutchDisengaged !=1)
break;
else clutchMotorPower==0
WHILE (clutchMotorPower==1 & sensorClutchEngaged !=1)
break;
else clutchMotorPower==0
I don't know much about the system you're working on, so this is going to be some bare-bones code. Anyway, let's go through this step-by-step.
Assumptions:
button is a variable that is 1 if the button is currently pressed and 0 otherwise.
motor is a variable that we set to 1 to turn the motor on, or 0 to turn it off.
sensor is a variable that is 1 when the sensor is activated, or 0 otherwise
First, we need code to toggle the motor state when the button is pressed. (Assume all code samples are inside a function called from your main loop)
//values of static variables are preserved between function calls
static char bActivateMotor = 0; //1 if we should activate motor, 0 otherwise
static char bButtonHeld = 0; //1 if button was pressed last loop
//(to differentiate between button being held and it being released and pressed again)
if(!button) {
bButtonHeld = 0; //if button is not being pressed, it can't be being held down.
}
if(!bActivateMotor) { //we aren't running the motor
if(button && !bButtonHeld) { //button was pressed this loop (not held from previous loop)
bButtonHeld = 1; //Don't toggle motor state next loop just because button was held down
bActivateMotor = 1; //we should be running the motor
}
else {
motor = 0;
}
}
if(bActivateMotor) { //not else because could be activated this loop
if(button && !bButtonHeld) { //button toggles motor; if we're running, stop.
bButtonHeld = 1;
bActivateMotor = 0;
}
else {
motor = 1;
}
}
Now, the next part is to stop the motor when the sensor has activated:
//values of static variables are preserved between function calls
static char bActivateMotor = 0; //1 if we should activate motor, 0 otherwise
static char bButtonHeld = 0; //1 if button was pressed last loop
//(to differentiate between button being held and it being released and pressed again)
if(!button) {
bButtonHeld = 0; //if button is not being pressed, it can't be being held down.
}
if(!bActivateMotor) { //we aren't running the motor
if(button && !bButtonHeld) { //button was pressed this loop (not held from previous loop)
bButtonHeld = 1; //Don't toggle motor state next loop just because button was held down
bActivateMotor = 1; //we should be running the motor
}
else {
motor = 0;
}
}
if(bActivateMotor) { //not else because could be activated this loop
if(button && !bButtonHeld) { //button toggles motor; if we're running, stop.
bButtonHeld = 1;
bActivateMotor = 0;
}
/////////////////////
// Added Code Here //
/////////////////////
else if(sensor) {
bActivateMotor = 0; //motor will shut off next loop
}
else {
motor = 1;
}
}
With the lack of specifics about your code, it's hard to figure out exactly what difficulties you might be having, but hopefully the algorithm above will be a good starting point.

Resources