⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adcboardexample.pbas

📁 adc implementation board embedded shows adc development with microcontroller
💻 PBAS
字号:
'*******************************************************************************
' microcontroller/compiler : P16F877A/mikroBasic 1.15
'
' Project: adcmcp3204
' This project is a simple demonstration of mcp3204 with P16F877A;
' with minor adjustments, it should work with any other PIC MCU
' that has SPI module on portc. The program displays results of
' AD conversion on LCD display.
'
' EasyPic2 notes:
'  LCD display is connected to portb
'  ADC board is connected to portc
'
' JUMPERS:
'   A/D Converter - Both Disabled
'   TX1 - Disabled ( Not Connected )
'   RX1 - Disabled ( Not Connected )
'   RA_E Pull - Disabled ( Not Connected )
'   RB Pull - Disabled ( Not Connected )
'   RC Pull - Disabled ( Not Connected )
'   RD Pull - Disabled ( Not Connected )
'   SW1 - All OFF
'   SW2 - All OFF
'   SW3 - All OFF
'*******************************************************************************}
program ADCBoardExample

dim  measurement as word
dim  LastValue as word
dim  txt as char[16]

sub procedure Init
  ' Init LCD
  TRISB = 0                                   ' Init TRISB for LCD
  Lcd_Init(PORTB)
  LCD_Cmd(LCD_Clear)
  Lcd_Cmd(LCD_CURSOR_OFF)
  LastValue = 1                               ' Initialize and make them
  measurement = 0                             '            different

  ' Init SPI
  SPI_init_advanced(Master_OSC_div64,           ' Initialize PIC as master
                    DATA_SAMPLE_END,            ' Data sample at end
                    CLK_IDLE_LOW,
                    LOW_2_HIGH)
  SetBit(PORTC,0)
  ClearBit(TRISC,0)
end sub

sub function getADC(dim channel as byte) as word           ' returns 0..4096
dim tmp as word
  ClearBit(PORTC,0)                            ' select mcp3204
  SPI_write($06)
  channel = channel << 6                     ' bits 7 & 6 define ADC input
  tmp = word(SPI_read(channel) and $0F) << 8 ' get ADC value
  result = tmp or SPI_read($0)
  SetBit(PORTC,0)
end sub

sub procedure ProcessValue(dim Value as word)            ' Writes Value to LCD
  if LastValue <> Value then                    ' Write Value only if different than previous
      txt = "    "                            ' Clear last value on LCD
      LCD_Out(2,1,txt)
      WordToStr(measurement,txt)               ' Convert word to string
      LCD_Out(2,1,txt)                   ' Output new value
      LastValue = Value                       ' Equalize the values
    end if
end sub


'main procedure
main:
  Init                                         ' Initialize SPI and LCD
LCD_Out(1,1,"ADC value:")
  while true
     measurement = getADC(0)                 ' Get ADC result from Channel 0
     ProcessValue(measurement)                ' Do something with measurement
     Delay_ms(100)                            ' Wait for a while
  wend
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -