Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Usart + Adc

Status
Not open for further replies.

Overclocked

Member
Ive gotten the ADC to work to control the blink rate of a LED, but now I want to try and read the ADC value via USART on my Junebug. Ive gotten it to "talk" with the computer, but It gives me Funky stuff (numbers/letters/symbols) instead of the values I want. Im using a PIC18F1320 and using the pots on the junebug as my voltage source for the ADC.

Code:
Device = 18F1320
Clock = 4 // 4MHz clock
Config OSC = INTIO2, WDT = OFF, LVP = OFF

Include "IntOsc4.bas"
Include "ADC.bas"
Include "USART.bas"
Include "convert.bas"

    
Function Get_ADC_Sample() As Float   // Function to grab the ADC sample

    result = ADC.Read(1)     //grab a voltage from channel 1 and convert it
    result = (result +1)*500
    result = result / 1024
End Function

SetBaudrate (br9600)

   
While True                         // Create an infinite loop
USART.Write ("DC Volts = ",Get_ADC_Sample ,13,10)
DelayMS (500)
        
Wend

Note "IntOsc4.bas" is the basic file for the internal osc running at 4Mhz
 
I've edited Gramo's ADC code to get it working with the Junebug.
**broken link removed**

PS I think the problem with your program has to do with float vs integer math. I've found myself having all sorts of weird results until I make sure all my variables are of the same type.
Code:
Device = 18F1320
Clock = 4
Config OSC = INTIO2, WDT = OFF, LVP = OFF

Include "USART.bas"
Include "ADC.bas"
Include "convert.bas"

Dim ADVal As Word
// Read the AD port and scale for 0.00 - 5.00 volts...
Function ADInAsVolt() As Word
    result = (ADC.Read(1) + 1) * 500 / 1024
End Function
// Start Of Program...
OSCCON = $62    ' 4MHz internal clock
SetBaudrate (br9600)

// main program loop...
While true
    ADVal = ADInAsVolt
    USART.Write("DC Volts = ", DecToStr(ADVal / 100), ".", DecToStr(ADVal, 2), " ",13,10)
    DelayMS(500)
Wend
 
Last edited:
I've edited Gramo's ADC code to get it working with the Junebug.
**broken link removed**

PS I think the problem with your program has to do with float vs integer math. I've found myself having all sorts of weird results until I make sure all my variables are of the same type.

Awesome, that works. Thanks =D
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top