Part 2 : How to build a USB thermometer with PIC 18F4550 or 18F2550 |
| Ecrit par Administrator, le 27-10-2007 12:48 |
| Pages vues |
15464  |
|
FRONTPAGE_NO_TRANSLATION_AVAILABLE On this second part, we are adding a temperature sensor to the circuit described in the first part of this tutorial.
Sensor and schematic You can use the very usual LM35 circuit (Datasheet), or many others circuits like MCP9701, TC1047...
The following schematic show how to plug the sensor on our USB experimentation board:
The firmware We will add some code for analog to digital convertion and temperature calculation in the file user.c
//Lets add includes for ADC and strings manipulations
#include <stdlib.h>
#include <adc.h>
//[...]
// We will write output data in this string
char input_buffer[64];
// [...]
void Exercise_Example(void)
{
int temp;
// [...]
else if(start_up_state == 3)
{
if(mUSBUSARTIsTxTrfReady())
{
//A0 set in input
TRISAbits.TRISA0 = 1;
memset(output_buffer, '\0', 64);
output_buffer[(6*5)] = '\r';
output_buffer[(6*5)+1] = '\n';
OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_0_TAD,
ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS,
0b1011);
SetChanADC(ADC_CH0);
Delay10TCYx( 50 );
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for ADC conversion
temp = ReadADC(); // Read result and put in temp
CloseADC(); // Disable A/D converter
temp=temp/7; //Convert in centigrad degrees
itoa(temp, output_buffer); // Convert to a string
mUSBUSARTTxRam((byte*)output_buffer,(6*5)+2);
start_up_state=0;
}
}
}//end Exercise_Example
The value in Celsius degree will be sent threw the USB bus when the button on RB4 will be pressed.
Under windows, you can use the hyperterminal utility (19200bd with a 20MHz cristal) to see the value. Under linux you can use cat /dev/ttyACM0 .
![[Note]](photos/note.png) | How to use a 18F2550 |
You can use the source code for 4550 with very few modifications. Just select the appropriate device in the MPLAB configure menu. Change the linker script, and set the configuration bits (like in this picture). Remove any references to port D in io_cfg.h.
Here is a picture of a small prototype using PIC 18F2550:
|
Commentaires utilisateurs (1)
Commentaires en langue: English (0), French (1) |
|
|