Adson 0 Posted February 4, 2014 Share Posted February 4, 2014 Hello there, I am doing a project where I have to read in a square wave and determine the frequency of the wave then output that number. I found out that I am using a 1MHz clock, then I set up an interrupt to count the edges for my square wave input. I don't think my interrupt is working though. I even changed it to a push button and tried to get it to turn on led's it didn't work. ps. I'm using energia btw. pps. I'm using msp430 revision 1.5 Hope someone can help me out Thanks a bunch!! if you don't know what energia is energia.nu is where you can find information My code so far //LED PINS int ledPin0 = 3;// select the pin for the LED int ledPin1 = 4; int ledPin2 = 5; int ledPin3 = 6; int ledPin4 = 8; //Inputs volatile int sensorPin = A7; // select the input pin for the potentiometer //Variables volatile int temp=0; //int count=0; volatile int count1=0; volatile int number=0; void setup() { // declare the ledPin as an OUTPUT: pinMode(ledPin0, OUTPUT); pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); pinMode(ledPin4, OUTPUT); pinMode(sensorPin, INPUT_PULLUP); //attachInterrupt(analogRead(sensorPin),inter,CHANGE); } void loop() { if (analogRead(sensorPin == HIGH)){ count1 = count1++; } else{ number = count1; count1 =0; } //Check for Frequency //number = 1000000/count1; //Make temp Variable to change temp = number; //Coverts that number to binary //Checks if it need to turn on light 5 if(temp >= 16){ digitalWrite(ledPin4, HIGH); temp = temp - 16; } else{ digitalWrite(ledPin4, LOW); } //Checks for light 4 if(temp >= 8){ digitalWrite(ledPin3, HIGH); temp = temp-8; } else{ digitalWrite(ledPin3, LOW); } //Checks for light 3 if(temp >= 4){ digitalWrite(ledPin2, HIGH); temp = temp - 4; } else{ digitalWrite(ledPin2, LOW); } //Checks for light 2 if(temp >= 2){ digitalWrite(ledPin1, HIGH); } else{ digitalWrite(ledPin1, LOW); } //Checks for light 1 if(temp >= 1){ digitalWrite(ledPin0, HIGH); } else{ digitalWrite(ledPin0, LOW); } delay(5); //count1 =0; } //Interuppt Routine //void inter() //{ //count1=count1++; //} Quote Link to post Share on other sites
chicken 630 Posted February 4, 2014 Share Posted February 4, 2014 Pin-Interrupts in Energia (and Arduino) are only for digital pins. RISING means the value of the pin changed from LOW to HIGH. You could use this to detect when a button is pressed. Adson 1 Quote Link to post Share on other sites
energia 485 Posted February 4, 2014 Share Posted February 4, 2014 make sure that you input pin is pulled up. You can do this with pinMode(sensorPin, INPUT_PULLUP); Also note that A5 is indicating an analog channel. A5 when using it in any of the digital API's translates to pin 5 which is P1.3. So rather than using int sensorPin = A5; I suggest that you use uint16_t sensorPin = P1_3. Since count is being increased in the interrupt routine, you will need to make it a volatile. Simply said, volatile means that the value can change at any time. so that would be volatile uint8_t count1; Below is a small example that will toggle the red LED when the button is pushed. int pin = RED_LED; volatile int state = LOW; void setup() { pinMode(pin, OUTPUT); pinMode(PUSH2, INPUT_PULLUP); attachInterrupt(PUSH2, blink, CHANGE); } void loop() { digitalWrite(pin, state); } void blink() { state = !state; } Adson 1 Quote Link to post Share on other sites
Adson 0 Posted February 4, 2014 Author Share Posted February 4, 2014 Ok, I just updated the code, still isn't working. Does the fact that i'm getting an actual signal and not a digital one mean I need to go about checking the edges differently? also @@energia I tried the code you posted and it didn't make the light blink, When i hit the button nothing happens. Maybe my board is messed up? Quote Link to post Share on other sites
energia 485 Posted February 4, 2014 Share Posted February 4, 2014 I doubt that you board is damaged. It could be of course that the chip is damaged. What chip is in the socket on the board? Quote Link to post Share on other sites
Adson 0 Posted February 4, 2014 Author Share Posted February 4, 2014 I am using chip M430G2553. Also I made some progress going to update the code. Starting to lean away from interrupts if i can. Going to try and put the count into the main function. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.