• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Short C code for PIC16F627A builds but won't run

Status
Not open for further replies.

Flyback

Well-Known Member
Hello,
Our short C code builds but won’t run properly. (please find it attached)
(the output RB5, never goes high, even though the pulse train is definitely getting to the input pin RA2)
It is written in XC8 C in MPLAB.
The microcontroller is PIC16F627A
Do you know why it doesn’t work as it should?

----------------------------------------------------------------------------------------------------
//This code simply has an input "looking"
//at a train of pulses.
//When one of the pulses goes low, a delay is done....
//and then an output is taken high.

// CONFIG
#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
#pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 4000000

#include <xc.h>
#include <stdint.h>



uint8_t count;
uint8_t count1;
uint8_t count2;

void main(void) {

//Setup ports
// TRISA = 0b 1011 1100; //ADC INS
// TRISB = 0b 00000001; //ZCD & DALIRX..really 0x09

TRISA = 0xBC;
TRISB = 0x01;

RA0 = 0; //NC
RA1 = 0; //NC
//RA2 = 0; //ZERO X ..input
//RA3 = 0; //on/off ..input
//RA4 = 0; //select zapper ..input
//RA5 = 0; //RA5 ..MCLR ..input
RA6 = 0; //RA6 ..LED failure ..output
//RA7 = 0; //failure ..input

//RB0 = 0; //SELECT OAMP ..INPUT
RB1 = 0; //LED ..ZAPPER ..OUTPUT
RB2 = 0; //LED OAMP ..OUTPUT
RB3 = 0; //NC ..OUTPUT
RB4 = 0; //ZAPPER SWITCH ..OUTPUT
RB5 = 0; //AC SWITCH ..OUTPUT
RB6 = 0; //PGC ...OUTPUT
RB7 = 0; //PGD ..OUTPUT

INTCON = 0x00;
//OPTION_REG = 0b 10000111
OPTION_REG = 0x87;
//VRCON = 0b00;
VRCON = 0x00;
CMCON = 0x07;

while (RA2 == 0);
while (RA2 ==1);

//At this point the pulse has just gone low

__delay_ms(3); //delay to get to the mains peak
__delay_us(600);

RB5 = 1;
__delay_ms(1);
RB5 = 0;

while(1){;}


return;
}
 

jpanhalt

Well-Known Member
Most Helpful Member
Building is the lowest bar possible. It is far easier to write code that builds, but doesn't work than code that builds and works -- I just spent a day finding a movlw that should have been movwf (20 pages). Silly, I know, but us lefties need something to take up our time.

Anyway, can you post the Assembly from the C?
 

Pommie

Well-Known Member
Most Helpful Member
You are driving RB5 high for 1 millisecond - will you even see that?

Where is the program counter when you pause the program?

Mike.
P.S. please learn how to use code tags.
 

JimB

Super Moderator
Most Helpful Member
What is the erring port pin driving?

I once had a bit of a head scratcher where a PIC output was doing some strange things because of the large capacitive load on the pin (a few 1000 pF).
Reduce the capacitance and it worked OK.

JimB
 

jpanhalt

Well-Known Member
Most Helpful Member
What is the erring port pin driving?

I once had a bit of a head scratcher where a PIC output was doing some strange things because of the large capacitive load on the pin (a few 1000 pF).
Reduce the capacitance and it worked OK.

JimB
Or, use the LATx registers? High capacitance on a port was/is the main cause of RMW error.
 

gophert

Well-Known Member
Most Helpful Member
Or, use the LATx registers? High capacitance on a port was/is the main cause of RMW error.

The old 16F627 didn't have LATx registers -just PORTx
 

jpanhalt

Well-Known Member
Most Helpful Member
The old 16F627 didn't have LATx registers -just PORTx
Microchip gives a workaround in its datasheets for chips that don't have LAT registers. The point was that you can to avoid the RMW problem by using code that is not susceptible to it.
 

gophert

Well-Known Member
Most Helpful Member
Microchip gives a workaround in its datasheets for chips that don't have LAT registers. The point was that you can to avoid the RMW problem by using code that is not susceptible to it.
I know there is a workaround, I just didn't want the rest of the world believing your post that specifically mentions LATx registers.
 

gophert

Well-Known Member
Most Helpful Member
And presumably you know what chip JimB was using?
No, I was simply trying to keep you focused on the OPs issue instead of spewing on other topics with hopes of filling your needs to get a pat on the head for being so worldly. It is not helpful.
 

Flyback

Well-Known Member
A workaround for RMW might i think be to write to the whole port, even if we only want to change one bit of the port?
Also, putting say a 10us delay between writes to different bits of the same port might help solve RMW issues?
Also, i am wondering if we need to use "PORTA,6" instead of "RA6"?
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top