• PicŪ Basic


  • MCP3204 12bit ADC SPI

    An example of using the Microchip MCP3204, 12 bit, 4 channel, ADC or the MCP3208, 12 bit, 8 channel, ADC
    Code:
    ' Read channel 0 of an MCP3204, 4 channel ADC and display
    ' the results on the serial terminal
    '
    Device = 16F628A
    Declare Xtal = 4            ' Use Internal clock
    ;-------------------------------------------------------------------------------
    ;**** Added by Fuse Configurator ****
    ; Use the Fuse Configurator plug-in to change these settings
    
    
    Declare Reminders Off
    @ CONFIG_REQ = 0 ; Override Compiler's configuration settings
    Asm-
    __Config  0x3F78 ;FOSC_INTOSCIO & WDTE_OFF & PWRTE_OFF & MCLRE_ON & BOREN_ON & LVP_OFF & CPD_OFF & CP_OFF 
    Endasm-
    Declare Reminders On
    
    ;**** End of Fuse Configurator Settings ****
    ;-------------------------------------------------------------------------------
    '
        TRISA = 00              ' PortA as output     
        TRISB = 00              ' PortB as output
        PORTA = 0               ' Clear PortA
        PORTB = 0               ' Clear PortB
        CMCON = 7               ' Comparators off 
    '                                                                        
    ;Calculated Baudrate = 9615 @ Xtal 4MHz, Error = 0.16%
    Declare Hserial_RCSTA = 144 ; Enable continuous receive
    Declare Hserial_TXSTA = 36  ; Enable transmit, BRGH = 1
    Declare Hserial_SPBRG = 25  ; Baud Rate Generator Value
    Declare Hserial_Clear = On  ; Clear overflow automatically
    '
    Symbol CS = PORTB.4
    Symbol Dout = PORTB.5
    Symbol Din = PORTB.6
    Symbol CLK = PORTB.7
    Dim ADCval As Word
    '
    '
    '
    ' Data out on falling edge of clock
    ' Transfer  MSB-> LSB
    ' 5 leading 0's, start bit, S/D, 3 bit Chan Sel, Null
    ' Data in on rising edge of clock MSB ->LSB
    ' Read in after Null the 4 Msb followed by the last 8 LSB's  
    '
        Low CS                                      ' Enable MCP3204
        DelayMS 100                                 ' Wait to stabilize
    '    
    '
    '
    Main:
    '
        Low CS
        SHOut Dout, CLK, MsbFirst, [%00000110000\11]' 5 leading, start bit, Single, ChanSel,Null
        SHIn Din, CLK, 0, [ADCval\13]               ' Shift in 13 bits.
        High CS
        HRSOut "ADCval= ", Bin12 ADCval,"    ADCval = ", Dec4 ADCval, 10, 13 
        ADCval = 0                                  '
        High CS                                     ' Turn off MCP3204                 
        DelayMS 250                                 '
        GoTo Main                                   '
    '
    End


    The above code can also be used to program the MCP3208.
  • Recent Activity

    towlerg-21522

    Mysterious PORTB problem

    Thread Starter: xldaedalus

    I'm using Proton+ to develop firmware for a product with switches. The MCU is an 18F26K22. Most of the switches reside on PORTB. I am NOT using a...

    towlerg Today, 15:19 Go to last post
    towlerg-21522

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    towlerg Yesterday, 15:38 Go to last post
    towlerg-21522

    Lin bus and auto baud detection

    Thread Starter: Tim

    Lin bus looks to be a great system for an application i'm working on. One of the main things is the slave can be really simple without needing a...

    towlerg Today, 15:13 Go to last post
    rcurl-44

    PIC18F1330 PLL problem

    Thread Starter: rcurl

    I'm just starting out on a new project that uses a PIC18F1330. I haven't used this specific chip before, so I began by trying to blink an LED just...

    rcurl Today, 14:11 Go to last post