PIC16F1934 Port Write-after-read issue

Status
Not open for further replies.

dougy83

Well-Known Member
Most Helpful Member
Hello all,

I have a little project based on a PIC16F1934, but I'm having a little bit of trouble using the port bits; specifically, setting a bit on PORTD clears all others on PORTD. If I single step through the BSF statements, I can see that the value on PORTD is read back as 0 always.

The voltage on the output pin when on is 4.6V (Vcc is 4.98V), so I don't know why it's reading back as '0'.

Is there any modules/peripherals that need to be disabled, e.g. mux LCD driver, to allow the outputs to be read back correctly & therefore allow BSF instructions to work as required?

Any other ideas?

Code attached for the initialisation & little test bit. (I've also tried stuffing NOPs in between the BSF commands to no avail)

Code:
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & CPD_OFF & BOREN_OFF & CLKOUTEN_OFF & IESO_ON & FCMEN_OFF);
__CONFIG(VCAPEN_RA5 & PLLEN_OFF & STVREN_ON & BORV_HI & LVP_OFF);

#define POWER_LED   RD2
#define DRIVER_LED  RD3
#define ERROR_LED   RD4

void initIo()
{
	CPSCON0 = 0;
    TRISA = 0b11101111;
    TRISB = 0b11111111;
    TRISC = 0b10000000;
    TRISD = 0b10000000;
    TRISE = 0b11111011;

    // MSSP -- expander shift register latches
    SSPSTAT = 0b00000000;
    SSPCON1 = 0b00110010;   // idle high (data ready for low-high transition), 312.5kHz
    SSPCON2 = 0b00000000;
    SSPCON3 = 0b00000000;
	WCOL = 0;
	SSPBUF = 0;				// shift out something to set the BF

    // UART
    TXSTA = 0b00100100;     // high speed async
    RCSTA = 0b10010000;
    BAUDCON = 0b00001000;   // 16 bit divider
    SPBRG = (uint8_t)BAUD_DIV_HS;
    SPBRGH = BAUD_DIV_HS >> 8;

    // ADC
    ANSELA = (uint8_t)ANSEL_VAL;
    ANSELB = (uint8_t)(ANSEL_VAL >> 8);
    ADCON0 = 0b00000001;    // AD ON
    ADCON1 = 0b10100000;    // right-justified, TAD=1.6us

    // Timers
    // TIMER 0 - 1ms timer
    OPTION_REG = 0b00000100;    // timer0 / 32

    // TIMER 1 - period = 340 cycles of current frequency
    T1CON = 0b00000001;		// timer 1 on, FOSC/4
    T1GCON = 0;
    TMR1 = 0;
    TMR1IF = 0;


    CCP1CON = 0;            // make sure it's off


    // CCP4 (lcd contrast) setup
    // use timer 4
    CCPTMRS0bits.C4TSEL = 1;    // use timer4 for PWM
    T4CON = 0;
    PR4 = 99;               // 0-100% duty cycle
    CCPR4L = 10;            // 10% to start with
    CCP4CON = 0b00001100;   // PWM
    T4CONbits.TMR4ON = 1;   // timer on
}

void main()
{
    initIo();

    // for testing
    POWER_LED = 1;  
	ERROR_LED = 1;
	DRIVER_LED = 1;

    delayms(3000);
    PORTD = 0xFF;
    delayms(3000);
    PORTD = 0;
 
This is normally caused by the port being in analogue mode.

Edit, just checked the data sheet and you need to clear ANSELD.

Mike.
 
Last edited:
Wow, thanks very much. I didn't realise that was required as there's no ADCs or comparators on there - but there is cap-sense!

Thanks again
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…