Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TestAnalogInPins by
main.cpp
- Committer:
- skanderian
- Date:
- 2016-05-11
- Revision:
- 5:b98a0f60f700
- Parent:
- 4:9e93bf4863cb
- Child:
- 6:214e2180c586
File content as of revision 5:b98a0f60f700:
/*TestAnalogInPins: written by Sami Kanderian, last updated on 11 May 2016. It continuously reads
and prints 16 Analog Input Pin voltages on the NPX Freescale FRDM-KL25Z microprocessor board via
serial inputs registered via an RX Interrupt trigger that changes the value of charCCIn, where
charCCIn goes from '00' to '16'. charCCIn should be preceeded by a '#'. Serial baud rate is 9600.
The data acquisition rate whereby voltage readings are updated is defined by the variable updatePeriodMs.
Serial inputs followed by Carriage Return:
'#00': print analog input voltages on all pins (deafault printout on startup)
'#01': print analog input voltage on pin PTE20
'#02': print analog input voltage on pin PTB0
'#03': print analog input voltage on pin PTE21
'#04': print analog input voltage on pin PTB1
'#05': print analog input voltage on pin PTE22
'#06': print analog input voltage on pin PTB2
'#07': print analog input voltage on pin PTE23
'#08': print analog input voltage on pin PTB3
'#09': print analog input voltage on pin PTE29
'#10': print analog input voltage on pin PTC2
'#11': print analog input voltage on pin PTE30
'#12': print analog input voltage on pin PTC1
'#13': print analog input voltage on pin PTC0
'#14': print analog input voltage on pin PTD1
'#15': print analog input voltage on pin PTD5
'#16': print analog input voltage on pin PTD6
*/
#include "mbed.h"
//Declare hardware inputs
Serial serial(USBTX, USBRX);
Timer timer;
//IMPORTANT NOTE: PTA1 and PTA2 ARE RESERVED FOR SERIAL COMMUNICATION VIA SDA USB!!! DO NOT USE FOR SENSOR/ACTUATOR I/O!!!
//Analog inputs : 0-3.3V
AnalogIn pin1(PTE20);//works as 16 bit ADC
AnalogIn pin2(PTB0); //works as 12 bit ADC
AnalogIn pin3(PTE21);//should be 16 bit ADC but doesnt work as ADC on mbed. Instead PTE21 reads whatever analog reading is input to PTE29
AnalogIn pin4(PTB1); //works as 12 bit ADC
AnalogIn pin5(PTE22);//works as 12 bit ADC
AnalogIn pin6(PTB2);//works as 12 bit ADC
AnalogIn pin7(PTE23);//works as 12 bit ADC
AnalogIn pin8(PTB3); //works as 12 bit ADC
AnalogIn pin9(PTE29);//works as 12 bit ADC
AnalogIn pin10(PTC2);//works as 12 bit ADC
AnalogIn pin11(PTE30);//works as 12 bit ADC
AnalogIn pin12(PTC1);//works as 12 bit ADC
AnalogIn pin13(PTC0);//works as 12 bit ADC
AnalogIn pin14(PTD1);// doesnt work. Seems to be stuck at 3.3Vz
AnalogIn pin15(PTD5);//works as 12 bit ADC
AnalogIn pin16(PTD6);//works as 12 bit ADC
//Built in LEDs
PwmOut rLed(LED_RED);
PwmOut gLed(LED_GREEN);
PwmOut bLed(LED_BLUE);
//New globals for RxInterrupt routine
const int bufferSize = 256;
char rxBuffer[bufferSize];
char txBuffer[bufferSize];
volatile int rxIn = 0;
volatile int rxOut = 0;
bool rxFlag = 0;
//other globals
char charCCIn[2];
int decCCIn;
int updatePeriodMs = 1000;
void ledConfirmSent()//Light up blue LED 10%
{
rLed = 1;
gLed = 1;
bLed = 0.9;
}
void ledConfirmReceive()//Light up green LED 10%
{
rLed = 1;
gLed = 0.9;
bLed = 1;
}
void sendAnalogIn(int pinNum) //send Analog input in V.
//Pin inputs are normalized to have a max value of 1 so a 3.3 multiplier is used to convert back to voltage
{
if (pinNum == 0 || pinNum == 1) {
serial.printf("%s%03.1f%s\r\n", "#PTE20: ", 3.3f*pin1.read(), "V");
}
if (pinNum == 0 || pinNum == 2) {
serial.printf("%s%03.1f%s\r\n", "#PTB0: ", 3.3f*pin2.read(), "V");
}
if (pinNum == 0 || pinNum == 3) {
serial.printf("%s%03.1f%s\r\n", "#PTE21: ", 3.3f*pin3.read(), "V");
}
if (pinNum == 0 || pinNum == 4) {
serial.printf("%s%03.1f%s\r\n", "#PTB1: ", 3.3f*pin4.read(), "V");
}
if (pinNum == 0 || pinNum == 5) {
serial.printf("%s%03.1f%s\r\n", "#PTE22: ", 3.3f*pin5.read(), "V");
}
if (pinNum == 0 || pinNum == 6) {
serial.printf("%s%03.1f%s\r\n", "#PTB2: ", 3.3f*pin6.read(), "V");
}
if (pinNum == 0 || pinNum == 7) {
serial.printf("%s%03.1f%s\r\n", "#PTE23: ", 3.3f*pin7.read(), "V");
}
if (pinNum == 0 || pinNum == 8) {
serial.printf("%s%03.1f%s\r\n", "#PTB3: ", 3.3f*pin8.read(), "V");
}
if (pinNum == 0 || pinNum == 9) {
serial.printf("%s%03.1f%s\r\n", "#PTE29: ", 3.3f*pin9.read(), "V");
}
if (pinNum == 0 || pinNum == 10) {
serial.printf("%s%03.1f%s\r\n", "#PTC2: ", 3.3f*pin10.read(), "V");
}
if (pinNum == 0 || pinNum == 11) {
serial.printf("%s%03.1f%s\r\n", "#PTE30: ", 3.3f*pin11.read(), "V");
}
if (pinNum == 0 || pinNum == 12) {
serial.printf("%s%03.1f%s\r\n", "#PTC1: ", 3.3f*pin12.read(), "V");
}
if (pinNum == 0 || pinNum == 13) {
serial.printf("%s%03.1f%s\r\n", "#PTC0: ", 3.3f*pin13.read(), "V");
}
if (pinNum == 0 || pinNum == 14) {
serial.printf("%s%03.1f%s\r\n", "#PTD1: ", 3.3f*pin14.read(), "V");
}
if (pinNum == 0 || pinNum == 15) {
serial.printf("%s%03.1f%s\r\n", "#PTD5: ", 3.3f*pin15.read(), "V");
}
if (pinNum == 0 || pinNum == 16) {
serial.printf("%s%03.1f%s\r\n", "#PTD6: ", 3.3f*pin16.read(), "V");
}
ledConfirmSent();
}
void runWhenNewSerialIn() {
if (rxBuffer[0] !='#') {
serial.printf("%s\r\n", "Input format should be '#XX'. First input character should be '#'");
serial.printf("%s\r\n", "followed by XX where XX goes from '00' to '16'");
}
for (int i = 0; i < 2; i++) {
charCCIn[i] = rxBuffer[i+1];
}
decCCIn=strtol(charCCIn,NULL,10);// this line converts char to int
//decCCIn= 10*((int)(charCCIn[0])-48)+ ((int)(charCCIn[1])-48);// equivalent to line above
serial.printf("%s%d\r\n", "decCCIn= ",decCCIn);
}
void Rx_interrupt() {
// Loop just in case more than one character is in UART's receive FIFO buffer
// Stop if buffer full
//while ((serial.readable()) && (((rxIn + 1) % bufferSize) != 0)) {
while (serial.readable()) {
rxBuffer[rxIn] = serial.getc();
if (rxBuffer[rxIn] == '\r') { //looking for character not string (string is double quotes) \r is CR, \n is LF
rxFlag = 1;
//Turn built in LED blue (at half intensity) to confirm command recieved
ledConfirmReceive();
//Execute runWhenNewSerialIn when new Rx recieved (ending with \r)
runWhenNewSerialIn();
rxFlag = 0; //reset flag to listen for next message
rxIn = 0; // reset position index to 0
} else {
rxIn = (rxIn + 1) % bufferSize;
}
}
}
int main() {
serial.baud(9600);
serial.attach(&Rx_interrupt, Serial::RxIrq);
timer.start();
//flash LED blue then green on startup or system reset
ledConfirmSent();
wait(0.2);
ledConfirmReceive();
while (1) {
timer.reset();
//Run sendAnalogIn in a loop. New decCCIn is registered with new serial input
sendAnalogIn(decCCIn);
//wait for updatePeriodMs to go by before printing analog voltage(s) again
while(timer.read_ms()<updatePeriodMs);
}
}
