Why is my C program skipping over this if statement? - c

I have this C program that I am writing in code composer studio.
#include <msp430.h>
/*
* main.c
*/
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
int R5_SW=0, R6_LED=0, temp=0;
P1OUT = 0b00000000; // mov.b #00000000b,&P1OUT
P1DIR = 0b11111111; // mov.b #11111111b,&P1DIR
P2DIR = 0b00000000; // mov.b #00000000b,&P2DIR
while (1)
{
// read all switches and save them in R5_SW
R5_SW = P2IN;
// check for read mode
if (R5_SW & BIT0)
{
R6_LED = R5_SW & (BIT3 | BIT4 | BIT5); // copy the pattern from the switches and mask
P1OUT = R6_LED; // send the pattern out
}
// display rotation mode
else
{
R6_LED = R5_SW & (BIT3|BIT4|BIT5);
// check for direction
if (R5_SW & BIT1) {// rotate left
R6_LED << 1;
} else {
R6_LED >> 1;
} // rotate right
// mask any excessive bits of the pattern and send it out
R6_LED &= 0xFF; // help clear all bits beyound the byte so when you rotate you do not see garbage coming in
P1OUT = R6_LED;
// check for speed
if (R5_SW & BIT2) {__delay_cycles( 40000); } //fast
else {__delay_cycles(100000); } //slow
}
}
}
When it gets to this if statment in debug mode
if (R5_SW & BIT1) {// rotate left
R6_LED << 1;
} else {
R6_LED >> 1;
} // rotate right
It skips over it, it doesn't run the if or the else block. At this point in the code R5_SW is 22 which is 0010 0010 in binary so R5_SW & BIT1 should evaluate to true. What am I missing here?

If you use an operation like << or >> without assigning it then the result is just discarded. Try this:
if (R5_SW & BIT1)
{
R6_LED = R6_LED << 1;
}
else
{
R6_LED = R6_LED >> 1;
}
Or, for brevity:
if (R5_SW & BIT1)
{
R6_LED <<= 1;
}
else
{
R6_LED >>= 1;
}

This piece of code...
if (R5_SW & BIT1) {// rotate left
R6_LED << 1;
} else {
R6_LED >> 1;
} // rotate right
evaluates either the expression R6_LED >> 1 or the expression R6_LED << 1 but it doesn't do anything with the result of it, so the compiler chooses to throw it away the entire IF statement.
A line such as a>>b by itself doesn't modify a. It is not like for example a++ which does modify a.

Related

MSP430 i var randomly resets at value 0 in middle of loop

So I got this very strange problem that happens every time. I am trying to interface and LCD with the MSP430 module. But in this function at the middle of the loop, the variable i resets itself to 0 for no apparent reason at all, somethimes it even crashes. This is the structure of the lcd_t struct:
struct lcd_t
{
uint8_t rs, rw, e;
uint8_t pin[8];
};
This is the function where i is defined and "reseted":
void
lcd_dat(const struct lcd_t* lcd, uint8_t dat)
{
static uint8_t i;
// Setting RS.
//
lcd_change_pin_state(lcd->rs, 1);
// Setting each data pin to match the dat.
//
for(i = 0; i < 8; i++) // TODO: Four bit mode
{
if(dat & (1 << i))
{
lcd_change_pin_state(lcd->pin[i], 1);
}
else
{
lcd_change_pin_state(lcd->pin[i], 0); <-- This is where i resets
}
}
// Setting E.
//
lcd_change_pin_state(lcd->e, 1);
__delay_cycles(2*1000);
// Clearing E.
//
lcd_change_pin_state(lcd->e, 0);
}
This is the function where i resets:
static volatile void
lcd_change_pin_state(uint8_t pin, uint8_t newstate)
{
if(newstate == 0)
{
if(pin/10 == 1)
{
P1OUT &= ~(1 << (pin % 10));
}
else
{
P2OUT &= ~(1 << (pin % 10));
}
}
else
{
if(pin/10 == 1)
{
P1OUT |= (1 << (pin % 10));
}
else
{
P2OUT |= (1 << (pin % 10));
}
}
}
Plese tell me what other information do you need! Thanks!
WDTCTL = WDTPW + WDTHOLD; // stop watchdog timer
This instruction was missing...
I can't even think how much time was lost because of this...

How can the average of the ADC Readings be calculated?

The aim is to store the newest 10 ADC readings in an array and then calculate the average of them to be used elsewhere. Removing the oldest each time it is updated.
Regarding the LED timing, it must switch timing from 1s to 0.25s if ADC reading is within boundaries as written below, how can this be implemented correctly? I know my method works but can be done better.
As for the LED's they must change patterns if a switch is pressed as you can see, which they do, but yet again I'm sure it can be done another simpler way!
Below is my code, Also I'm sure there are many an error and plenty of room for optimization, I will gladly accept it all!
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
unsigned int timecount0;
unsigned int adc_reading;
volatile uint32_t timing = 1;
volatile uint32_t accumulator = 0;
volatile uint16_t average = 0;
volatile uint16_t samples = 0;
#define LED_RED PORTB = ((PORTB & ~0b00001110)|(0b00000010 & 0b00001110))
#define LED_GREEN PORTB = ((PORTB & ~0b00001110)|(0b00001000 & 0b00001110))
#define LED_BLUE PORTB = ((PORTB & ~0b00001110)|(0b00000100 & 0b00001110))
#define LED_RGB PORTB = ((PORTB & ~0b00001110)|(0b00001000 & 0b00001110))
#define DELAY_COUNT 6
volatile uint8_t portdhistory = 0xFF;
void Timer0_init(void)
{
timecount0 = 0; // Initialize the overflow count. Note its scope
TCCR0B = (5<<CS00); // Set T0 Source = Clock (16MHz)/1024 and put Timer in Normal mode
TCCR0A = 0; // Not strictly necessary as these are the reset states but it's good
// practice to show what you're doing
TCNT0 = 61; // Recall: 256-61 = 195 & 195*64us = 12.48ms, approx 12.5ms
TIMSK0 = (1<<TOIE0); // Enable Timer 0 interrupt
PCICR |= (1<<PCIE0);
PCMSK0 |= (1<<PCINT0);
sei(); // Global interrupt enable (I=1)
}
void ADC_init(void)
{
ADMUX = ((1<<REFS0) | (0<<ADLAR) | (0<<MUX0)); /* AVCC selected for VREF,ADLAR set to 0, ADC0 as ADC input (A0) */
ADCSRA = ((1<<ADEN)|(1<<ADSC)|(1<<ADATE)|(1<<ADIE)|(7<<ADPS0));
/* Enable ADC, Start Conversion, Auto Trigger enabled,
Interrupt enabled, Prescale = 32 */
ADCSRB = (0<<ADTS0); /* Select AutoTrigger Source to Free Running Mode
Strictly speaking - this is already 0, so we could omit the write to
ADCSRB, but included here so the intent is clear */
sei(); //global interrupt enable
}
int main(void)
{
ADC_init();
Timer0_init();
DDRD = 0b00100000; /* set PORTD bit 5 to output */
DDRB = 0b00111110; /* set PORTB bit 1,2,3,4,5 to output */
sei(); // Global interrupt enable (I=1)
while(1)
{
if(!(PIND & (1<<PIND2)))
{
PORTD = PORTD |= (1<<PORTD5);
PORTB = PORTB |= (1<<PORTB4);
if(average>512)
{
PORTB = PORTB |= (1<<PORTB5);
}
}
else
{
PORTD = PORTD &= ~(1<<PORTD5);
PORTB = PORTB &= ~(1<<PORTB4);
}
}
}
ISR(TIMER0_OVF_vect)
{
TCNT0 = 61; //TCNT0 needs to be set to the start point each time
++timecount0; // count the number of times the interrupt has been reached
if(!(PIND & (1<<PIND3)))
{
if (timecount0 >= 0) // 40 * 12.5ms = 500ms
{
PORTB = ((PORTB & ~0b00001110)|(0b00000000 & 0b00001110));
}
if (timecount0 >= 8*timing)
{
LED_RED;
}
if (timecount0 >= 16*timing)
{
LED_GREEN;
}
if (timecount0 >= 24*timing)
{
PORTB = ((PORTB & ~0b00001110)|(0b00000110 & 0b00001110));
}
if (timecount0 >= 32*timing)
{
PORTB = ((PORTB & ~0b00001110)|(0b00001000 & 0b00001110));
}
if (timecount0 >= 40*timing)
{
PORTB = ((PORTB & ~0b00001110)|(0b00001010 & 0b00001110));
}
if (timecount0 >= 48*timing)
{
PORTB = ((PORTB & ~0b00001110)|(0b00001100 & 0b00001110));
}
if (timecount0 >= 56*timing)
{
PORTB = ((PORTB & ~0b00001110)|(0b00001110 & 0b00001110));
}
if (timecount0 >= 64*timing)
{
timecount0 = 0;
}
}
else
{
if (timecount0 >= 0)
{
PORTB = ((PORTB & ~0b00001110)|(0b00000000 & 0b00001110)); //ALL OFF
}
if (timecount0 >= 8*timing)
{
LED_RED;
//PORTB = ((PORTB & ~0b00001110)|(0b00000010 & 0b00001110)); //RED
}
if (timecount0 >= 16*timing)
{
LED_GREEN;
}
if (timecount0 >= 24*timing)
{
LED_BLUE;
}
if (timecount0 >= 32*timing)
{
timecount0 = 0;
}
}
}
ISR (ADC_vect) //handles ADC interrupts
{
adc_reading = ADC; //ADC is in Free Running Mode
accumulator+= adc_reading;
if ((adc_reading > 768) & (adc_reading <= 1024))
{
timing = 10;
}
if ((adc_reading >= 0) & (adc_reading<= 768) )
{
timing = 2.5;
}
samples++;
if(samples == 10)
{
average = accumulator/10;
accumulator = 0;
samples = 0;
}
}
Depending on your processors, you may way to keep ISR() speedy and avoid expensive /,%.
The LED stuff, I'd handle in a timer interrupt.
#define N 10
volatile unsigned sample[N];
volatile unsigned count = 0;
volatile unsigned index = 0;
volatile unsigned sum = 0;
ISR (ADC_vect) {
if (count >= N) {
sum -= sample[index];
} else {
count++;
}
sample[index] = ADC;
sum += sample[index];
index++;
if (index >= N) {
index = 0;
}
}
unsigned ADC_GetAvg(void) {
block_interrupts();
unsigned s = sum;
unsigned n = count;
restore_interrupts();
if (n == 0) {
return 0; //ADC ISR never called
}
return (s + n/2)/n; // return rounded average
}
I'd recommend an integer version of a low pass filter than the average of the last N.
In terms of the moving averaging w/ N = 10, chux - Reinstate Monica has provided the solution. Chux - Reinstate Monica also recommends looking at an integer version of a low pass filter. I personally like the Exponentially Weighted Moving Average (EWMA) because it's fairly simple to code and only requires a few values to do the averaging. This is compared to having to hold 10 in array in your case. I would recommend Elliot Williams's Make: AVR Programming Chapter 12 for this. In case you don't have access to this readily, the EWMA, as explained in Make AVR, starts with
y_current = (1/16)*x_current + (15/16)*y_previous
where in our case, y_current is the updated EWMA value, x_current is the newest sample from your ADC, and y_previous is the last EWMA value. The choice of 16 can also be changed along with the weights, 1 and 15. Keeping it a power of 2 is important though, as you will see. As shown in Elliot Williams book, you multiply by 16 and compensate for rounding problems and get the following,
16*y_current = x_current + 16*y_previous - (16*y_previous - 8)/16.
Now, I know this looks ugly but what we have is scaled by 16 average value that's an integer and only relies on integer addition (the 16*y_previous is stored as one value so you don't do the multiplication) and a bit shift; that's the reason why a power of 2 was chosen in the EWMA, dividing by 16 is the same as a right bit shift of 4. Ok, so what does this average look like in code:
// Snippet from Make: AVR Programming
uint16_t x_current; // ADC value.
uint16_t y_current; // Average ADC value.
// Get the EWMA.
y_current = x_current + y_current - ((y_current - 8) >> 4);
// Send the value over USART (assuming it's wired up). Remember that
// y_current is scaled by 16.
printf("%d\n",(y_current>>4));
The above is just the EWMA that you can use in your code and an example of sending it, which is just a reminder that the value if scaled. Remember, this is just the averaged ADC value. Likely you will be wanting to use the ADC value as an input to a function to get the value of some measured quantity. Rather than actually using a function and calculating values, you can create a look-up table where the index is the ADC value and the array entry at that index is the precalculated value.
In terms of your other code, the things that could be corrected/streamlined are in your ISRs. In ISR(TIMER0_OVF_vect) you have some bit operations that are constant and can be precalculated so that your not doing it everytime the ISR(TIMER0_OVF_vect) fires.
PORTB = ((PORTB & ~0b00001110)|(0b00000000 & 0b00001110));
becomes
PORTB = ((PORTB & 0b11110001)|(0b00000000)); // Saves a bit inversion and '&'
which shows that your ORing, |, doesn't affect the result, because you're ORing against all zeros.
Finally, in your ISR (ADC_vect) you are using the bitwise, &, and not the logical and, &&. You get the same result but it's like using a wrench to hammer in a nail. I know this is a lot but I hope it helps and let me know if you need clarification.

click counter to light up LEDs using C code

I'm designing a click counter for a microcontroller and I'm using C for developing the code.
What happens here is when I click first button it will count the number of button presses. Then I have to press the second button to display the count in binary lighting up the LEDs. For example, if I press first button 10 times, it will light up second LED and fourth LED.
Since there are 8 LEDs I'm using 2 different ports.( 6 bits from PORTB and 2 bits from PORTD).
For that I have developed a code using if/else.But I want to implement this operation without multiple if/else operations because this method doesn't seem like much efficient.
while(PIND & 0b00100000){ //while PD5 switch is not pressed
if(clickCount>=128){ //if click count>=128
PORTB = PORTB | 0b00100000; //set PB5 HIGH
clickCount-=128; //deduct 128 from clickCount
}else if(clickCount>=64){
PORTB = PORTB | 0b00010000;
clickCount-=64;
}else if(clickCount>=32){
PORTB = PORTB | 0b00001000;
clickCount-=32;
}else if(clickCount>=16){
PORTB = PORTB | 0b00000100;
clickCount-=16;
}else if(clickCount>=8){
PORTB = PORTB | 0b00000010;
clickCount-=8;
}else if(clickCount>=4){
PORTB = PORTB | 0b00000001;
clickCount-=4;
}else if(clickCount>=2){
PORTD = PORTD | 0b10000000;
clickCount-=2;
}else if(clickCount==1){
PORTD = PORTD | 0b01000000;
clickCount = 0;
}
}
And I want to make this code in less number of bytes. So is there any way to develop this code segment using a for loop or any other method?
I don't know if this reduces the binary size, and not tested. You can still do something like this:
unsigned char mask[] = {
0b01000000, 0b10000000, 0b00000001, 0b00000010,
0b00000100, 0b00001000, 0b00010000, 0b00100000};
while(PIND & 0b00100000) {
for (int i = 7, v = 128; i > -1; --i, v /= 2) {
if (clickCount >= v && clickCount > 0) {
if (clickCount >= 4) {
PORTB = PORTB | mask[i];
} else {
PORTD = PORTD | mask[i];
}
clickCount -= v;
break;
}
}
}
Or you can use a single loop:
int v = 128, i = 7;
while (v > 0 && (PIND & 0b00100000)) {
if (clickCount >= v) {
if (clickCount >= 4) {
PORTB = PORTB | mask[i];
} else {
PORTD = PORTD | mask[i];
}
clickCount -= v;
} else {
--i;
v /= 2;
}
}
This could be done by just assigning values to PORTB and PORTD seperately since you have the number of clicks stored in a variable-clickCounter.
PORTB = PORTB | (clickCount & 0b00111111);
PORTD = PORTD | (clickCount & 0b11000000);
To handle the output, I would do something like:
clickCount %= 256; /* because the output is 8-bit */
PORTB = clickCount;
To make the output exactly 1 instruction long:
unsigned char clickCount = 0;
... /* do things, handle increment, handle 2nd button */
PORTB = clickCount; /* modulo 256 no longer needed, ClickCount cannot be more than 8 bits anyway */
I see no reason for doing it bit by bit.

trying to let the motor stop when press the two push button

I am working on a project and it has two push buttons, each one of them turns the motor in a direction (clock/anti clock wise) via H-bridge, however I am trying to let the motor stop when I click both of the push buttons. Everything is working fine but I can't get the two push buttons part...
Here is my attempt:
#include "motor.h"
#include <avr/io.h>
#include <util/delay.h>
int flag=0;
int main(void)
{
DDRD &= ~(1<<PD2);
DDRD &= ~(1<<PD3);
DDRB |= (1<<PB0);
DDRB |= (1<<PB1);
PORTB &= ~(1<<PB0);
PORTB &= ~(1<<PB1);
while(1)
{
if (PIND & (1<<PD2))
{
flag = 1;
}
else if (PIND & (1<<PD3))
{
flag = 2;
}
else if (PIND & (1<<PD3 ) && (1<<PD2))
{
flag = 3;
}
switch (flag)
{
case 1:
DC_MOTOR_PORT |= (1<<DC_MOTOR_PIN1);
DC_MOTOR_PORT &= ~(1<<DC_MOTOR_PIN2);
break;
case 2:
DC_MOTOR_PORT |= (1<<DC_MOTOR_PIN2);
DC_MOTOR_PORT &= ~(1<<DC_MOTOR_PIN1);
break;
case 3:
DC_MOTOR_PORT=0x00;
break;
}
}
}
I assume that when you press one button, the 1 << PD2 pin in PIND becomes set. The condition PIND & (1 << PD2) becomes true, the if branch is taken, and the code doesn't bother to test other conditions (since they are under else clauses).
Besides, && is a logical operation. (1<<PD3 ) && (1<<PD2) always yields true, so the else clause would effectively test for PING & 1. Not exactly what you want.
Consider instead
button_state = PIND & ((1 << PD2) | (1 << PD3)); // Mask out pins of interest
switch (button_state) {
case 0: // Nothing pressed
....
break;
case 1 << PD2: // One button pressed
....
break;
case 1 << PD3: // Another button pressed
....
break;
case (1 << PD2) | (1 << PD3): // Both pressed
....
break;
In the else-if construct, the last condition will never be tested because the first test PIND & (1<<PD2) will also be true. Also the use of && in the third test is incorrect.
A further problem is that you read PIND multiple times. To ensure consistency you should read it once and test the value read.
You could test for the third condition (both buttons) first, but that is in fact unnecessary. The whole construct can be replaced with:
uint8_t buttons = PIND ;
flag = 0 ;
if( buttons & (1<<PD2) )
{
flag |= 1;
}
if( buttons & (1<<PD3) )
{
flag |= 2 ;
}
Or more succinctly perhaps:
flag = ( buttons & (1<<PD2) ) ? 1 : 0 ;
flag |= ( buttons & (1<<PD3) ) ? 2 : 0 ;
Then when both are pressed, flag will be 1 | 2 which is 3.
Even that is probably unnecessary; you could just use the masked value of PIND directly in the switch. That solution has already been posted by #user58697.

C - atmega328p - I don't understand what is wrong

i do not unterstand why my code isnt working(doISR). What its supposed to do:
-there is a running light(6 LEDs) and 3 Buttons. I need to press the button at the right time(Button 1, if LED 1 or 2 is on....etc). if its correct: increase the speed, if not: reset.
i bet its a blunder, but i dont get it :)
void wait(void) {
while (!(TIFR1 & (1 << OCF1A))) // wait
;
TIFR1 |= (1 << OCF1A); // delete Interrupt flag
}
volatile bool ISRhasHappend = false;
ISR(PCINT0_vect) {
ISRhasHappend = true;
}
int main(void) {
OCR1A = 3125 - 1; //2 seconds
TCCR1B |= (1 << WGM12); //turn on CTC mode
TCCR1B |= (1 << CS12) | (1 << CS10); //Prescalemode 101 -> 1024 Takt
DDRB = (1 << PCINT5);
DDRC = 0x3f;
PCICR = 0x3f; //Pin Change Interrupt Control Register
PCMSK0 = (1 << PCINT0) | (1 << PCINT2) | (1 << PCINT4);
sei();
doRunningLight();
if (ISRhasHappend == true) {
doISR();
}
}
void doISR() {
//while timee >0
for(int x=3125;x>0;x=x-250){
//if LEDs 0+1 on, and button0 pressed ...etc
if ((PORTC & (0b00000011)) && (PINB & (1 << PINB0)) || (PORTC &
(0b00001100)) && (PINB & (1 << PINB2)) || (PORTC & (0b00110000)) &&
(PINB & (1 << PINB4))) {
//All led lights up
PORTC |= 0x3f;
wait();
//reduce timer if catched the light
OCR1A = x;
}
else {
//turn signal
for (int y = 1; y < 5; y++) {
PORTB ^= (1 << PCINT5);
wait();
}
//back to 3124 if failed
OCR1A = 3125 - 1;
}
}
ISRhasHappend = false;
}
void doRunningLight(){
while(1) {
for (int i = 0; i<6; i++){
PORTC |= (1<<i);
wait();
PORTC &= ~(1<<i);
}
}
}
The function doRunningLight() never returns, so the code that you wrote after it to check ISRhasHappend and call doIsr will never run. You will need to think of a different way to handle your events, probably without using blocking delay loops.
I would also question whether you actually need a pin-change interrupt. Button presses are slow enough that your microcontroller should be able to detect them by reading the pin in the main loop.

Resources