Showing the right value from ADC

General discussion on mikroBasic PRO for 8051.
Post Reply
Author
Message
gadelhas
Posts: 77
Joined: 09 Apr 2010 01:09

Showing the right value from ADC

#1 Post by gadelhas » 05 May 2010 01:00

Hi everyone once again;

I need to do a program that converts me the value get from de ADC ( 0...4096) to a value from 0 to 5V, rounded to decimal, for exemple; 0; 0.5; 1; 1.5...4.5; 5.

I Allready do the code bellow, and allready can read the value from the adc(0...4096). However i can't display the correct value.

Can anybody help me once again;

Here is the code;

Code: Select all

program Trabalho5
' ****************************************************************************
'
' Trabalho 5 - UART + DS1820 + ADC
' ======================
'
' Author: Hugo Oliveira
' Date: Maio, 2010
' ****************************************************************************
'                             Configuração de Pinos
dim GLCD_DataPort as byte at P0
dim GLCD_CS1 as sbit at P2_0_bit
dim GLCD_CS2 as sbit at P2_1_bit
dim GLCD_RS  as sbit at P2_2_bit
dim GLCD_RW  as sbit at P2_3_bit
dim GLCD_EN  as sbit at P2_4_bit
dim GLCD_RST as sbit at P2_5_bit

dim ADC_CS as sbit at P3_5_bit

dim valorADC as word
dim valorADCS as string[23]
dim valorADCF as Float

'                             Funções e Subrotinas
' ====================================================================
'                             Aquisição de ADC
' ====================================================================
sub function getADC(dim channel as byte) as word    ' returns 0..4096
dim tmp as word

  ADC_CS = 0                                        ' select MCP3204
  SPI1_Write(0x06)

  channel = channel << 6
  result = SPI1_read(channel) and (0x0F)            ' bits 7 & 6 define ADC input
  result = result << 8

  result = result or SPI1_read(0)                   ' get ADC value
  ADC_CS = 1
end sub

'                       Inicialização de Periféricos
' ====================================================================
sub procedure Init

  Glcd_Init()
  Glcd_Fill(0x00)
  Glcd_Set_Font(@FontSystem5x7_v2, 5, 7, 32)
  SPI1_Init()
  ADC_CS = 1                       ' deselect ADC chip

end sub

'                          PROGRAMA PRINCIPAL
' ====================================================================
main:
  Init()


  while (TRUE)
    valorADC = getADC(0)        ' get ADC result from Channel 0
    WordToStr(valorADC,valorADCS)
    Glcd_Write_Text(valorADCS,15,4,1)

    
    ValorADCF=((valorADC*5.0)/4096.0)
    FloatToStr(valorADCF, valorADCS)
    Glcd_Write_Text(valorADCS,15,3,1)
    delay_ms(500)
  wend

end.
Thanks and Regards
Gadelhas

gadelhas
Posts: 77
Joined: 09 Apr 2010 01:09

Re: Showing the right value from ADC

#2 Post by gadelhas » 10 May 2010 22:25

Someone??? :D
Thanks and Regards
Gadelhas

Post Reply

Return to “mikroBasic PRO for 8051 General”