FliiADC_1 - Flinders University Wiki
Download
Report
Transcript FliiADC_1 - Flinders University Wiki
Flii Programming
Workshop
Overview
*.X Project
LEDs
MPLAB X
ICD 3
Docking Station
LCD Screen
Switches
PIC Microcontroller
Potentiometer
• commonly referred to as a pot
• a three-terminal resistor with a sliding
contact that forms an adjustable voltage
divider
• commonly used to control electrical
devices such as volume controls on
audio equipment
The Wiki
Download
Flii Base Project V2.0
with Boot-loader
Open the ZIP file
drag the folder inside it
onto the Desktop
Open Flii Library V2.0
Function Descriptions
MPLAB X
Open MPLABX IDE
Go to File Open Project
Find your Flii Base Project
folder and open it
Under Project, expand
Source Files and open
user.c
Things To Remember
• Case sensitivity
– MyVariable is not the same as myvariable
• Underlined things = syntax errors
– Using the wrong variable name
– Using the wrong function
• Check if you’re using BufferWriteString(…) or BufferWriteMessage(…)
– Missing brackets, semi-colons, etc
Preparation
• Update definitions
– open Board_defs.h
– find #define SPARE_ADC
– change the 5 to a 4
• Add variables to user.c:
underneath extern unsigned char string[];
unsigned double pot = 0;
unsigned double milliVolts = 0;
unsigned int x = 0;
unsigned int y = 0;
Reading from Potentiometer
• What function would you use to read from the spare
input? (i.e. the potentiometer)
unsigned int ReadADC ( char channel, char reference_voltage );
• To what variable will we assign the returned value?
What channel and reference voltage will we use?
What will our code look like?
pot = ReadADC(SPARE_ADC, POS_REF_BATT);
Convert Raw to Millivolts
The ADC has 10 bits meaning 2^10 or 1024
The maximum value is equivalent to the reference voltage
(approx 3V using 2 x AA batteries)
i.e. our range is Min = 0 to Max = 3000mV
Therefore each ADC bit is equal to
3000mV / 1024 bits = 2.93mV
Our code to convert the raw value to millivolts is
milliVolts = pot * 2.93;
Write Millivolts to Screen
Clear the buffer:
BufferClear();
If writing a number, convert it to characters:
itoa(milliVolts, (char*) string);
Write your message:
BufferWriteString((char*) string, RIGHT, 0, 0, NORMAL);
Also display a label on the left side of the screen that says:
Input mV:
HINT: Use BufferWriteMessage
Turn LED On & Off
• Write code so that LED 1 turns on if the
potential is more than halfway and turns
off again if it drops back down
– Use an if/else statement
if (milliVolts > 1500) {
LED1On();
} else {
LED1Off();
}
Graph the Results
• Save the top 8 pixels for text
– Leaves 40 vertical pixels for graph
• Max milliVolts = 3000, so each pixel represents
3000/40 = 75 milliVolts
y = milliVolts / 75; //divide maximum millivolt value by 75
y = (40 - y);
//subtract from 40 so a value of 0 is at the
//bottom and a value of 3000 is at the top
y = (y + 7);
//offset by 7 so we don't write over our text
Graph the Results
• Draw a pixel at the new location
– Can draw 4 pixels in a square if you prefer
– BufferWritePixel(x, y, PIXEL_ON);
• Draw a horizontal line at the halfway point
– What are the x1 and x2 values? What is the y value?
– As before: (1500/75) + 7 = 27
– BufferWriteHLine(0, 84, 27, PIXEL_ON);
• Write the buffer to the screen
– writeBufferToScreen();
Graph the Results
• What about the x value?
– We want the dot to move across with time
– Therefore, increment x each time we loop with:
X++;
(This is shorthand for x = x+1;)
• But! This will move really fast! What can we do?
– Put in a delay, approximately 300ms is good
delayMs(300);
Graph the Results
• What happens when we reach the end of the screen?
– Our dot disappears off the edge!
• Task: Test if the x value is too big and reset to 0 if it is
– Use an if/else statement and clear the buffer
• Extension:
– What happens if you don’t clear the buffer at all?
– What happens if you change the delay time?
– When you reset, also draw a line showing the average
potential over the last iteration