Shiftin problem (16f877A with MCP3201)


Closed Thread
Results 1 to 4 of 4
  1. #1

    Default Shiftin problem (16f877A with MCP3201)

    I have my LM35DZ connected to a MCP3201. The connections between MCP & 877A are as follows:
    CLK - PortE.0
    Dout - PortE.1
    CS - PortE.2
    My code is as follows:
    Code:
    '-----------------Defines & Includes--------------------------------------------
    Include "modedefs.bas"
    DEFINE SHIFT_PAUSEUS 50
    DEFINE	OSC	4
    
    '------------------------Configuration Fuses------------------------------------
    #CONFIG
        ifdef PM_USED
            device  pic16F877A, xt_osc, wdt_on, lvp_off, protect_off
        else	
    		__CONFIG _XT_OSC & _WDT_OFF & _LVP_OFF & _CPD_ON & _CP_ALL & _PWRTE_ON & _BODEN_ON
       endif
    #ENDCONFIG
    
    
    @ ERRORLEVEL -306			' SUPRESS PAGE BOUNDRY ERROR
    
    DEFINE DEBUGIN_REG	PORTC
    DEFINE DEBUGIN_BIT	7
    DEFINE DEBUG_REG	PORTC
    DEFINE DEBUG_BIT	6
    DEFINE DEBUG_BAUD	2400
    DEFINE DEBUG_MODE	1
    
    '======================================
    '--------------------REGISTERS-------------------------------
    ADCON1=7 ' NO ANALOG
    CMCON=7						' No Compapator
    OPTION_REG=128
    TRISA=0 : PORTA=0
    TRISB=0 : PORTB=0
    TRISC=0 : PORTC=0
    TRISD=0 : PORTD=0
    TRISE=%00000010 : PORTE=0
    CCP1CON=0
    
    '-- ShiftIN/Out pins
    CLK	Var PortE.0
    DIN Var PortE.1
    CS	Var PortE.2
    '-------------------
    
    CS=1
    Sensor=0
    
    Main:
    
    		CS=0 : pauseus 100
    		SHIFTIN Din,Clk,6,[Sensor\12]
    		CS=1
    		DEBUG DEC Sensor,13,10
    		PAUSE 1000
    Goto Main
    But my readings are not working properly. I get in my terminal window the following:

    Code:
    38
    19
    3091
    3091
    3091
    3091
    3091
    3091
    3091
    3091
    3091
    3091
    3091
    3091
    3091
    .
    .
    .
    According to my calculations, I should get around 154 as 154 x 1.22(mV) is around 18.8 degrees which seems correct. Please help me identify what have I missed. Thanks
    ___________________
    WHY things get boring when they work just fine?

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Shiftin problem (16f877A with MCP3201)

    I think you need mode 4 in the SHIFTIN.
    And it needs to shift 16 bits, not 12. Even though it's a 12-bit result.

    A few other changes and ... try this ...
    Code:
    #HEADER
        ERRORLEVEL -306			; SUPRESS PAGE BOUNDRY ERROR
    #ENDHEADER
    
    #CONFIG
        __CONFIG _XT_OSC & _WDT_OFF & _LVP_OFF & _CPD_ON & _CP_ALL & _PWRTE_ON & _BODEN_ON
    #ENDCONFIG
    
    DEFINE DEBUGIN_REG	PORTC
    DEFINE DEBUGIN_BIT	7
    DEFINE DEBUG_REG	PORTC
    DEFINE DEBUG_BIT	6
    DEFINE DEBUG_BAUD	2400
    DEFINE DEBUG_MODE	1
    
    '-- MCP3201 pins
    CLK	  Var PortE.0
    DIN   Var PortE.1
    CS	  Var PortE.2
    
    '-------------------
    Calibration  CON -1            ; Signed value in 0.1 deg. C
    Sensor       VAR WORD
    
    '-------------------
    ADCON1 = 7
    CMCON = 7
    HIGH CS
    
    Main:
        LOW CS
        SHIFTIN Din,Clk,4,[Sensor\16]
        HIGH CS
    
        Sensor = (Sensor & $FFF) * 5000
        Sensor = DIV32 4095 + Calibration
        IF Sensor <= 1500 THEN DEBUG DEC Sensor/10,".",DEC1 Sensor,13,10
        PAUSE 1000
    Goto Main
    Last edited by Darrel Taylor; - 17th November 2013 at 00:38.
    DT

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Shiftin problem (16f877A with MCP3201)

    Thanks a lot for the help. It is working. But could you explain my doubts please?

    Quote Originally Posted by Darrel Taylor View Post
    I think you need mode 4 in the SHIFTIN.
    I understand why clock idles high (as data comes on falling edge), BUT why read before sending clock mode?
    Quote Originally Posted by Darrel Taylor View Post
    And it needs to shift 16 bits, not 12. Even though it's a 12-bit result.
    Should this be a normal practice or is there something I missed?

    Quote Originally Posted by Darrel Taylor View Post
    A few other changes and ... try this ...
    Code:
    '-------------------
    Calibration  CON -1            ; Signed value in 0.1 deg. C - Not sure what this does?
    Sensor       VAR WORD
    
    '-------------------
    HIGH CS
    
    Main:
        LOW CS
        SHIFTIN Din,Clk,4,[Sensor\16]
        HIGH CS
    
        Sensor = (Sensor & $FFF) * 5000 ' Understand the first part, we masked the high 4 bits and force them to 0 BUT I dont get the remaining maths, like Why times by 5000? and what does the below statement do
        Sensor = DIV32 4095 + Calibration
        IF Sensor <= 1500 THEN DEBUG DEC Sensor/10,".",DEC1 Sensor,13,10
        PAUSE 1000
    Goto Main
    ___________________
    WHY things get boring when they work just fine?

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    _____ _____ _____ _____ _____ _____ _____ _____
    The need for 16-bits comes from the MCP3201 datasheet ...


    Name:  MCP3201.gif
Views: 1881
Size:  24.6 KB

    _____ _____ _____ _____ _____ _____ _____ _____
    I didn't overthink the mode number. Just tried different ones till it worked.

    Name:  LM35_MCP3201_16F877A.jpg
Views: 1904
Size:  85.4 KB
    DT

Similar Threads

  1. SHIFTIN SHIFTOUT timing problem
    By BrianT in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 11th April 2011, 21:08
  2. Problem with 16F877A but not with 16F876A
    By savnik in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 16th May 2008, 14:18
  3. 16f877A and shiftin shiftout
    By ghdsfier8 in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 22nd February 2008, 13:47
  4. SHIFTIN problem
    By champion in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 29th January 2007, 03:31
  5. 16F877A HSERIN problem
    By RFsolution in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 30th October 2006, 02:51

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts