Swordfish Tutorial - ADC (Control LED Blink Speed)

This tutorial/example covers another quick guide on how to use Analogue to Digital conversions with a PIC micro. It's achieved by interfacing with a 1K potentiometer to determine how fast the LED will blink. Some other aspects covered in this example include the use of the 18F4550 with the internal oscillator and basic function use.

LED20Blink20via20ADC1

Simulation video of circuit

 

Basically the PIC micro will sample AN0 (PORTA.0) and get a reading from 0 to 1023 (0V to 5V). From there, I use the DelaymS command to alter the time that the LED is on and off for. Its very basic, but does cover a couple of underlying features such as the use of a function;

Device = 18F2550 // Setup the device/clock information
Clock = 8
Config FOSC = INTOSCIO_EC
 
Include "ADC.bas" // Include the ADC.bas library
 
Dim LED as PORTC.0 // Declare the LED pin
 
 
function Get_ADC_Sample() as word // Function to grab the ADC sample
 
 result = ADC.Read(0) // Grab an ADC sample from channel 0
 result = result * 2 // Scale the answer up by a factor of 2
 
end function
 
 
OSCCON = %01110110 // Setup the internal OSC for 8Mhz
 
Input(PORTA.0) // Make AN0 an input
 
While True // Create an infinate loop
 
 High(LED) // Make the LED pin High
 Delayms(100 + Get_ADC_Sample) // Delay for 0.1 seconds + the ADC result
 
 Low(LED) // Make the LED pin Low
 Delayms(100 + Get_ADC_Sample) // Delay for 0.1 seconds + the ADC result
 
Wend 

 

Simulation;

 


Posted: 6 years 9 months ago by yllis #12629
yllis's Avatar
Is there a way to use some other port than PORTA?
Lets say that I would like to use PORTB.0, would I need to change the adc.bas somehow?
Posted: 6 years 9 months ago by majenko #12632
majenko's Avatar
You'd just change the ADC channel.
   result = ADC.Read(0)             // Grab an ADC sample from channel 0

into
   result = ADC.Read(12)             // Grab an ADC sample from channel 12

You see on the schematic above, that RB0 is AN12, so you can read from analog channel 12.

Forum Activity

Member Access