MSP430 as a Freq Gen Square Wave
Posted: Thu Apr 04, 2013 12:11 pm
Thought id share some code, used a program called Energia to code this up for the MSP430 Launchpad, it's a 3.3volt device but a diode inline with the output is enough to make it work with a ECU/PCM, i am currently using this for MAF gen. Has a simple button up/down to change the Freq clock.
I am only using the baseline msp430g2231 on the launchpad, very cheap device!
http://www.ti.com/product/msp430g2231
http://www.ti.com/tool/msp-exp430g2
I am only using the baseline msp430g2231 on the launchpad, very cheap device!
http://www.ti.com/product/msp430g2231
http://www.ti.com/tool/msp-exp430g2
Code: Select all
/*
The1's Square Wave FREQ Gen
nigel@eyeit.org
Push Button UP/Down
1hz increment
0-30,000hz Range
3.3V
P1_3 UP Button
P1_4 DOWN Button
P1_6 FREQ OUT
*/
const int buttonP1_3 = P1_3; // Define UP Button PIN
int buttonUP = 0; // Set UP Button to 0
const int buttonP1_4 = P1_4; // Define DOWN Button PIN
int buttonDOWN = 0; // Set DOWN Button to 0
int TempFreq = 2000; // Set Bootup Freq
int FreqInc = 1; // Pushbutton Freq Increment
void setup() {
// initialize the Buttons as an input:
pinMode(buttonP1_3, INPUT_PULLUP);
pinMode(buttonP1_4, INPUT_PULLUP);
}
void loop()
{
// Refresh for Bootup or Reset
analogFrequency (TempFreq) ; // Refresh Output on Bootup or Reset
analogWrite(P1_6, 127); // Send PWM to Pin P1_6 50% duty cycle
// Button UP Code
buttonUP = digitalRead(buttonP1_3); // read the state of the UP Button:
if (buttonUP == LOW) // check if the pushbutton is pressed = LOW.
{
TempFreq = TempFreq + FreqInc; // Add FreqInc to Current Output
analogFrequency (TempFreq) ; // Send New Output
analogWrite(P1_6, 127); // Send PWM to Pin P1_6 50% duty cycle
}
// Button DOWN Code
buttonDOWN = digitalRead(buttonP1_4);// read the state of DOWN Button:
if (buttonDOWN == LOW) // check if the pushbutton is pressed = LOW.
{
TempFreq = TempFreq - FreqInc; // Subract FreqInc From Current Output
analogFrequency (TempFreq) ; // Send New Output
analogWrite(P1_6, 127); // Send PWM to Pin P1_6 50% duty cycle
}
}