a/d converter

General discussion on mikroPascal.
Post Reply
Author
Message
borisnovak
Posts: 25
Joined: 12 Mar 2009 20:43

a/d converter

#1 Post by borisnovak » 30 Mar 2009 11:14

I tried to make a program that will represent on leds RD0-RD7 the digital value of RA1 analog signal imput.
Can someone correct my code so it will work.

Code: Select all

program test;
label start;
begin

TRISA := $FF;           // set PORTA to be input
TRISD := $00;           // set PORTD to be output
portd:=$00;

start:
if (porta.1>0)  and (porta.1<32) then    portd:=%00000001;
if (porta.1>32)  and (porta.1<64) then   portd:=%00000010;
if (porta.1>64)  and (porta.1<96) then   portd:=%00000100;
if (porta.1>96)  and (porta.1<128) then  portd:=%00001000;
if (porta.1>128)  and (porta.1<160) then portd:=%00010000;
if (porta.1>160)  and (porta.1<192) then portd:=%00100000;
if (porta.1>192)  and (porta.1<224) then portd:=%01000000;
if (porta.1>224)  and (porta.1<255) then portd:=%10000000;
goto start;
end. 
Last edited by borisnovak on 31 Mar 2009 11:45, edited 1 time in total.

alm7100
Posts: 85
Joined: 09 Oct 2008 11:58
Location: Denmark

#2 Post by alm7100 » 30 Mar 2009 13:01

Hi borisnovak.
if (porta.1>0) and (porta.1<32) then portd:=%00000001;
if (porta.1>32) and (porta.1<64) then portd:=%00000010;
[ /quote]

porta.1 is the same as Port A bit 1 and a bit is on 1 or 0
So skip the .1

Hope its help.
//
Allan (Sorry for my English.)

borisnovak
Posts: 25
Joined: 12 Mar 2009 20:43

#3 Post by borisnovak » 30 Mar 2009 20:56

alm7100 wrote:Hi borisnovak.
if (porta.1>0) and (porta.1<32) then portd:=%00000001;
if (porta.1>32) and (porta.1<64) then portd:=%00000010;
[ /quote]

porta.1 is the same as Port A bit 1 and a bit is on 1 or 0
So skip the .1

Hope its help.
I tried like this but it doesn't work.
As I know PIC16F887 has 14 channel A/D converter of 10bit resolution. And if I understand any, this means that you can connect 14 different analog inputs that can be converted to 10bit (1024 different values)digital value. EasyPic5 has two potenciometers that are conected to RA1-RA5 of portA pins. (I don't know why to the same pins).

So as I want to convert analog input from first potentiometer of RA1 pin that's why I read only the second pin of porta.

alm7100
Posts: 85
Joined: 09 Oct 2008 11:58
Location: Denmark

#4 Post by alm7100 » 30 Mar 2009 21:20

This is from the help

Code: Select all

program ADC_on_LEDs;

var temp_res : word;

  begin
    ANSEL  := 0x04;              // Configure AN2 pin as analog
    TRISA  := 0xFF;              // PORTA is input
    ANSELH := 0;                 // Configure other AN pins as digital I/O
    TRISC  := 0x3F;              // Pins RC7, RC6 are outputs
    TRISB  := 0;                 // PORTB is output

    while (TRUE) do
      begin
        temp_res := Adc_Read(2);   // Get 10-bit results of AD conversion
        PORTB := temp_res;         // Send lower 8 bits to PORTB
        PORTC := word(temp_res shr 2);   // Send 2 most significant bits to RC7, RC6
      end;
end;
Hope this will help you.
//
Allan (Sorry for my English.)

borisnovak
Posts: 25
Joined: 12 Mar 2009 20:43

#5 Post by borisnovak » 31 Mar 2009 18:43

yes this works!
I tried to make a working program that will show exact digital binary value of RA2 pin onto LCD but I don't know why the below source code doesn't work.

Code: Select all

//NOTE
//put jumper J1 to pull down position
//turn off DIP switch RA3
//put jumper on RA2 of A/D converter

program AD_converter;

label start;
var x,y:array[1..10] of word; //from 0-65,536
    i:byte;

begin
    ANSEL  := %00000100;              // Configure AN2 pin as analog
    ANSELH := 0;                      // Configure other AN pins as digital I/O
    TRISA  := $FF;                    // PORTA is input
    Lcd_Config(PORTB,3,2,1,0,PORTB,4,7,5);  //Initializes LCD data port and ctrl port with pin settings you specify.


LCD_out(2,1,'RA2 dig value');     //writes "RA2 dig value
start:
y[10] := Adc_Read(2);   // Get 10-bit digital value from RA2 analog pin
for i:=10 to 1 do
begin
     x[i]:=y[i] mod 2;
     y[i-1]:=y[i] div 2;
end;

for i:=1 to 10 do
if x[i]=1 then LCD_Out(1,i,'1') else LCD_Out(1,i,'0'); //show value on LCD
goto start;
end.
However the AD converter does work, as I tested it with this simplified version.

Code: Select all

//NOTE
//put jumper J1 to pull down position
//turn off DIP switch RA3
//put jumper on RA2 of A/D converter

program AD_converter;

label start;
var a: word; //from 0-65,536
    i:word;

begin
    ANSEL  := %00000100;              // Configure AN2 pin as analog
    ANSELH := 0;                      // Configure other AN pins as digital I/O
    TRISA  := $FF;                    // PORTA is input
    Lcd_Config(PORTB,3,2,1,0,PORTB,4,7,5);  //Initializes LCD data port and ctrl port with pin settings you specify.


LCD_out(2,1,'RA2 dig value');     //writes "RA2 dig value" onto 2nd line of LCD
start:
a := Adc_Read(2);   // Get 10-bit digital value from RA2 analog pin
if (a<%0000000011) and (a>%0000000010) then LCD_out(1,1,'%0000000010');
if (a<%0000000111) and (a>%0000000100) then LCD_out(1,1,'%0000000100');
if (a<%0000001111) and (a>%0000001000) then LCD_out(1,1,'%0000001000');
if (a<%0000011111) and (a>%0000010000) then LCD_out(1,1,'%0000010000');
if (a<%0000111111) and (a>%0000100000) then LCD_out(1,1,'%0000100000');
if (a<%0001111111) and (a>%0001000000) then LCD_out(1,1,'%0001000000');
if (a<%0011111111) and (a>%0010000000) then LCD_out(1,1,'%0010000000');
if (a<%0111111111) and (a>%0100000000) then LCD_out(1,1,'%0100000000');
if (a<%1111111111) and (a>%1000000000) then LCD_out(1,1,'%1000000000');
goto start;
end.

Post Reply

Return to “mikroPascal General”