stm32F4 7-segment display - c

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++;

Related

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.

Arduino Switch to Turn a Relay timer

Briefly: I would like to turn on a relay for 30 seconds, after I toggle a switch on.
I'm trying to do a blinds automation at home.
I have a simple ON-OFF-ON switch, attached to an Arduino connected to Relays.
I want to turn on Relay#1 for a maximum of 30 seconds if I toggle the switch down from center. In other words, relay turns on when I switch, and when timer reaches 30 seconds relay turns off.
similarly I want to turn on Relay#2 for exactly 30 seconds if I toggle the switch up from center
And when I switch back to center, I would like the timer to reset.
I could not figure out how. Could anyone help?
I have been trying to use elapsedMillis library for this, which is a nice library that helps me avoid using Delays:
http://playground.arduino.cc/Code/ElapsedMillis
However even though I could work the relays without the 30 second limitation, I couldn't figure out the code to end working of the relays. Here is my current code:
#include <elapsedMillis.h>
#define RELAY_ON 0
#define RELAY_OFF 1
#define RELAY1_TURNS_ON_BLINDS 5
#define RELAY2_SHUTS_DOWN_BLINDS 6
#define shutswitch A0
#define openswitch A1
bool LocalCommandToOpen;
bool LocalCommandToShut;
void setup() ////////SETUP////////
{
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
pinMode(RELAY1_TURNS_ON_BLINDS, OUTPUT);
pinMode(RELAY2_SHUTS_DOWN_BLINDS, OUTPUT);
pinMode(shutswitch, INPUT);
pinMode(openswitch, INPUT);
} ////SETUP
void loop() { ///////LOOP
if (digitalRead(shutswitch) == 1)
{
LocalCommandToOpen = 1;
}
else
{
LocalCommandToOpen = 0;
}
if ( digitalRead(openswitch) == 1)
{
LocalCommandToShut = 1;
}
else
{
LocalCommandToShut = 0;
}
unsigned int CloseInterval = 14000;
elapsedMillis timeElapsedSinceCloseButtonPush = 0;
unsigned int OpenInterval = 14000;
elapsedMillis timeElapsedSinceOpenButtonPush = 0;
//MANUAL SWITCH OPERATION
if ( LocalCommandToShut == 1 )
{
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_ON);
}
else
{
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
}
//MANUEL DUGME ILE ACMA
if ( LocalCommandToOpen == 1)
{
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_ON);
}
else
{
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
}
delay(500);
} /////////////////LOOP////////////////////////////////////
One suggestion here is to use a "state machine", so that upon a switch transition, you get 'State 1'; in that state, a timer starts, and puts you in 'State 2'. In 'State 2', you check the time, and if it goes beyond X seconds, you go to 'State 3'. You can monitor the transition of a switch from low to high (or high to low), and use this to reset the state of the system.
A sample bit of code gives you an idea of how to implement this. The variable 'SystemState' is an integer, and SYSTEM_ABORT, SYSTEM_IDLE, etc. are constants.
The beauty of this is that the transition is easy to figure out. For example, if you are in the SYSTEM_WAIT state, the only thing you are looking for is the time to be greater than 5 seconds. Also, you can look at input transitions or values to set states (like SystemStopButton == 0, to set the state to be 'SYSTEM_ABORT').
// SystemStopButton is an input
void SystemStateMachine(void)
///////////////////////////////////////////////////////////////////////////////////////////
{
if (SystemStopButton == 0)
{
SystemState = SYSTEM_ABORT;
}
switch (SystemState)
{
case SYSTEM_IDLE:
{
RunPinState = OFF;
StopPinState = OFF;
if (SystemRunButton == 0)
{
SystemState = SYSTEM_START;
ShowStep();
}
break;
}
case SYSTEM_START:
{
StandardMessage = "START ";
RunPinState = ON;
StopPinState = OFF;
SystemState = SYSTEM_WAIT;
ShowStep();
break;
}
case SYSTEM_WAIT: // wait for 5 seconds
{
StandardMessage = "WAIT ";
if ((CurrentMillis - StateStepTimeMillis) > 5000)
{
SystemState = SYSTEM_RETRACT;
ShowStep();
}
break;
}
case SYSTEM_RETRACT: //
{
StandardMessage = "RETRACT";
/* motor stuff goes here... */
if ((CurrentMillis - StateStepTimeMillis) > 5000)
{
SystemState = SYSTEM_ADVANCE_TAPE_WAIT
ShowStep();
}
break;
}
// etc. etc. etc.
case SYSTEM_ABORT: //
{
StandardMessage = "ABORT";
/* motor stop stuff goes here... */
SystemState=SYSTEM_IDLE;
break;
}
default:
{
break;
}
}
}
void ShowStep(void)
/////////////////////////////////////////////////////////////////////
// show step and set time so we can keep track of time in each step
{
Serial.print("SystemState = ");
Serial.println(String(SystemState));
SetStepTime();
}
void SetStepTime(void)
/////////////////////////////////////////////////////////////////////
{
StateStepTimeMillis = CurrentMillis;
}
You might use a state machine; this makes things a bit easier to follow.
Similar to:
Arduino Switch to Turn a Relay timer
A nice discussion of state machines is here:
Complicated state transitions: best practices

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.

nested switch case not working [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to execute some commands using USART communication and reading some values using ATmega32-A. have a look at my code . My problem is i am trying to add some cases (nested switch case) but i am not getting what i want. I hope i am giving all most all information required to solve my problem.
void uniCom(void) {
switch (Command) {
/* ... */
case (muxsel):
printf(muxselection);
switch (c) {
case 1:
printf("this is mux chaneel1");
DDRB = 0b10111111;
PORTB = 0b00000000;
printf("adc Value", ReadAdc());
Command = 0;
break;
case 2:
/*-------------------*/
break;
}
Command = 0;
break;
/* ... */
default:
Command = 0;
break;
}
}
The problem is undefined c. And I don't see any declaration of muxselection, maybe missing "" ? Now the second approach.
void selcase(void) {
unsigned char c;
printf("muxselection");
while (rx_counter0) {
c = getchar();
switch (c) {
case 1:
printf("this is mux chaneel1");
DDRB = 0b10111111;
PORTB = 0b00000000;
printf("adc Value", ReadAdc());
Command = 0;
break;
case 2:
/*-------------------*/
break;
}
}
}
void uniCom(void) {
switch (Command) {
/* ... */
case (muxsel):
printf(muxselection);
selcase();
Command = 0;
break;
/* ... */
default:
Command = 0;
break;
}
}
My problem is i am executing all the commands as i declared but i want to select some more cases in one of the main switch case command "muxsel". for that i wrote nested switch case. if i select "muxsel" command on hyperteminal then it is printing like "muxselection" then if i enter 1 to select "case '1'"in second switch, nothing is printing. it is printing "command not found". what is the problem. I want execute nested switch but i am not able to do that using above code I have tried like this also.
void selcase(void) {
unsigned char c;
printf("muxselection");
while (rx_counter0) {
c = getchar();
switch (c) {
case '1':
printf("this is mux chaneel1");
DDRB = 0b10111111;
PORTB = 0b00000000;
printf("adc Value", ReadAdc());
c= 0;
break;
case '2':
/*-------------------*/
break;
default;
break;
}
}
}
creating one function for nested switch case and calling in main switch case as shown below.
void uniCom(void) {
switch (Command) {
/* ... */
case (muxsel):
printf(muxselection);
selcase();
Command = 0;
break;
/* ... */
default:
Command = 0;
break;
}
}
this way also not working please suggest me how to overcome this problem. i want to select one of the command in main switch case such as "muxsel" after that i have select mux channels using case statement. any help appreciated.
Thanks in advance.
I have solved this problem.
OK... the code isn't terribly clear, but I think I see your problem.
You tried to modify the code like this:
case (muxsel):
printf(muxselection);
switch (c) {
case 1:
printf("this is mux chaneel1");
DDRB = 0b10111111;
PORTB = 0b00000000;
printf("adc Value", ReadAdc());
Command = 0;
break;
case 2:
First, you don't have c declared in the scope of the uniCom() function. So that would not compile. Because you didn't give full code I assume you know that and probably really did something like this:
void uniCom(void) {
unsigned char c;
c = getchar();
switch (Command) {
case (no_com):
Command = 0;
....
case (muxsel):
printf("muxselection\n"); //Need quotes here and maybe a \n?
switch (c) {
case 1:
...
Which leads to the next problem. You're asking for a char c but your cases are built on an int. For example, if the user enters 3, what you're getting is the character '3' or the int 51. Check the ASCII Table
So your cases are for start of header (SOH), start of text (STX), etc right now... that's not going to work the way you wanted it to. You need to either do this:
switch (c) {
case 51: // This is ASCII '1'
...
break;
case 52: // This is ASCII '2'
Or do this:
switch (c) {
case '1':
...
break;
case '2':
Since you didn't give your input, or how c was defined, I could be wrong... but I'm going to wager that's your problem. By the way, make sure you have a default case at the end with a message like "bad input", it makes this type of thing easier to catch.
EDIT:
Modify the code as follows and share the result:
void runCom(void){
unsigned char c;
c = getchar();
printf("%c %d\n", c, c); //<-- add this line here
switch(Command){
and
void selcase(void) {
unsigned char c;
printf("muxselection");
while (rx_counter0) {
c = getchar();
printf("%c %d\n", c, c); //<-- and this line here
switch (c) {
I wrote it as an answer 'cause there is no place for it in comments. Although it's definitely not a final satisfactory once, it might help you get there.
For now I see two problems. You say you tried altering case(muxsel). Your code cannot even compile in my opinion. The code for the function should look similar to this:
void uniCom(void) {
switch (Command) {
/* ... */
case (muxsel):
printf(muxselection);
switch (c) {
case 1:
printf("this is mux chaneel1");
DDRB = 0b10111111;
PORTB = 0b00000000;
printf("adc Value", ReadAdc());
Command = 0;
break;
case 2:
/*-------------------*/
break;
}
Command = 0;
break;
/* ... */
default:
Command = 0;
break;
}
}
The problem is undefined c. And I don't see any declaration of muxselection, maybe missing "" ?
Now the second approach.
void selcase(void) {
unsigned char c;
printf("muxselection");
while (rx_counter0) {
c = getchar();
switch (c) {
case 1:
printf("this is mux chaneel1");
DDRB = 0b10111111;
PORTB = 0b00000000;
printf("adc Value", ReadAdc());
Command = 0;
break;
case 2:
/*-------------------*/
break;
}
}
}
void uniCom(void) {
switch (Command) {
/* ... */
case (muxsel):
printf(muxselection);
selcase();
Command = 0;
break;
/* ... */
default:
Command = 0;
break;
}
}
The second case could compile probably. The thing I do not understand is, why in the 2nd case you do manually read c. While in the former you do not ? Should it be loaded within uniCom routine or not ? It sure is in getCom(). Maybe you should not mess with buffers from the level of uniCom or below. Also are you sure that while(rxcounter0) will ever stop ? And if getchar() means to read a character from standard input from a human user typing on a keyboard it might be hard to read 1 or 2 from him as these are pretty low ASCII codes not present there.

How to break an infinite for(;;) loop in C?

I have a vital infinite for loop that allows a sensor to keep updating its values. However I would like to break that for loop when another sensor brings in new values. How can I switch from one infinite for loop to another?
Current code:
for(;;){
SON_Start();
// Wait 65ms for max range time
delay10ms(7);
// Read Range
i = SON_Read(SON_ADDRESSES[sonarReading]);
// pause
delayMs(100);
if(i<15)
drive(200, RadCW);
}
What I would like to add:
If Sensor2 returns a reading (e.g. Sensor2 > 20), then I want to break the loop and goto another infinite for loop to begin a new function.
If you are looking for "switching between 2 infinite loops" it could be "wrapped" by third loop and this "switching" could be done by simple break.
But since you want your program to stop some day, this loop could be placed within the function and you could use return; for ending it:
void myMagicLoop()
{
for(;;)
{
for(;;)
{
if ( I should stop )
return;
if ( I should switch to second loop )
break;
}
for(;;)
{
if ( I should stop )
return;
if ( I should switch back to first loop)
break;
}
}
}
And somewhere you just call:
myMagicLoop();
Hope this helps.
This will switch between loop A and loop B.
for (;;)
{
// Loop A
for (;;)
{
if WANT_TO_SWITCH
{
break;
}
}
// Loop B
for (;;)
{
if WANT_TO_SWITCH
{
break;
}
}
}
You use break; to break a loop and pass control beyond its closing brace. For example
for(;;) {
if( whatever ) {
break;
}
}
//break gets you here
Alternatively you could consider rewriting this with an event-driven approach. This will of course depend on what your hardware is capable of, but at the very least you should be able to produce some timer events.
Then the code would go something like this:
static volatile bool sensor_1_ready;
static volatile bool sensor_2_ready;
for(;;)
{
switch(state_machine)
{
case READING_SENSOR_1:
if(sensor_2_ready)
{
state_machine = READING_SENSOR_2;
}
else if(sensor_1_ready)
{
process sensor 1
}
break;
case READING_SENSOR_2:
if(!sensor_2_ready && some_timeout_etc)
{
state_machine = READING_SENSOR_1;
}
else if(sensor_2_ready)
{
process sensor 2
}
break;
}
}
void callback_sensor_1 (void) // some sort of interrupt or callback function
{
sensor_1_ready = true;
}
void callback_sensor_2 (void) // some sort of interrupt or callback function
{
sensor_2_ready = true;
}
(Before commenting on the volatile variables, please note that volatile is there to prevent dangerous compiler optimizations and not to serve as some mutex guard/atomic access/memory barrier etc.)
The "break" command should do what you need?
The best way to do that is to change the for statement to something like:
for (; Sensor2 <= 20;) {
...
Alternatively you can change it from a for to a while statement:
while (Sensor2 <= 20) {
...
If that doesn't suite your needs you can always use a break instead.
Another option could be to use signals (SIGUSR1,SIGUSR2) to switch from one loop to another.
Something of this sort:
void sensor2(int signum)
{
for (; ;)
...
/* Need to process sensor 1 now */
kill(pid, SIGUSR1);
}
void sensor1(int signum)
{
for (; ;)
...
/* Need to process sensor 2 now */
kill(pid, SIGUSR2);
}
int main()
{
/* register the signal handlers */
signal(SIGUSR1, sensor1);
signal(SIGUSR2, sensor2);
...
}

Resources