Need help with PIC 16F57

Thread Starter

alex2016

Joined Oct 16, 2016
3
Sorry if this is not the place to write but here it goes.
Story: I'm working at a small to medium size company where we sell interphones and security devices (burglar systems, CCTV equipment, etc). Some of these interphones comes with PIC 16F57 microcontrollers which, depending on customer requirement, i switch the number of digits and number of seconds, for unlocking the door, by programming the microcontroller with already defined .hex files which we got from the manufacturer.
I'm doing this using PICKIT 3 and just importing them to the microcontroller.
Lately, some of our customers, asked if we can provide some "easy and cheap to implement" devices to control the timing of their interphones. Some of them need more than 1-8 seconds provided by factory settings of our interphones, not just for unlocking the door, but to control lights and other stuff.
These people dont want to invest another 50-100 dollars into an equipment that does that, so they are asking me if i have a solution for some extra bucks.
This basically was my excuse to dig deep into the idea of controlling relays and LED's which i kinda wanted for some time.
For the last couple of weeks i spent hours and hours searching on the internet on how to do simple commands using pic microcontroller, but i made almost no improvement so far.
So, i'm asking you guys if you could help me with some information, or links where i can learn to work with PIC16f57
The reason why i chosen PIC16F57 is because i have huge supply of them at the office. Any other model of microcontroller would be hard to get by.
Keep in mind, i'm a total noob when it comes to microcontrollers and programming (in any language).

What i've done so far was making a LED blink, 2 sec on, 2 sec off.
Equipment: Pickit 3
Software: MPLAB X IDE with xc8 compiler, and pickit 3 standalone programmer (i use pickit 3 standalone programmer instead of mplab to program the microcontroller because i cant do it with mplab)

What i'm trying to do: using an input (a simple switch) to turn on a relay(or LED) for 30 minutes more or less.
This is my code for my blinking LED which was succesful.

Code:
#pragma config OSC = HS         // Oscillator selection bits (HS oscillator)
#pragma config WDT = ON         // Watchdog timer enable bit (WDT enabled)
#pragma config CP = OFF         // Code protection bit (Code protection off)

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

#include <xc.h>

#define _XTAL_FREQ 12000000

void main(void) {
    TRISB = 0x00;
        while(1){
        PORTB = 0xFF;
        __delay_ms(2000);
        PORTB = 0x00;
        __delay_ms(2000);
     
    }
}
I'm gonna attach a schematic with my working blinking LED just to see what kind of connections i made.
I've seen plenty of examples but for other pic microcontrollers which didn't worked or implement in my case.
 

Attachments

takao21203

Joined Apr 28, 2012
3,702
ive used 16f5x a lot.

The FLASH on these is only 2K and the RAM is small.

Id rather use a DS1307 clock IC as timebase than implement the crystal oscillator and develope timing code, so you could use the square wave output of the IC to drive the timer, or access it via I2C.

For a professional security device this IC might be insufficient it may be enough if the circuit PCB can not be accessed but since the FLASH is small the code will be relatively simple and easy to guess.

You should think about anti tampering measures and dummy ICs on the PCB, if accessed modified an alert is set so you do something like double accounting. You could connect multiple 16F57 with serial links to make it quite hard to make any sense of the circuit board and file off the type numbers as well and dont have ICSP on the silkscreeen.

What exactly is your question? To implement a timing routine in C language? My suggestion is to use a clock IC to have a timebase available otherwise you need to choose suitable crystal and calculate and keep a lot of variables for the time.
 

Thread Starter

alex2016

Joined Oct 16, 2016
3
Thanks for fast response, but i realize now i wrote things unnecessary.

I need to program pic to open a relay for more than 30 minutes. Simple, it doesnt have to be pretty, nor to have any type of security level or to be precise. It could be any value between 30 and 60 minutes, just more than 30 min.
So what i'm trying to do:
If i press 1 button, the microcontroller to give me an output for minimum of 30 min.

My question is: What is the code in MPLAB or any other program that supports pic16f57 for this operation ?

Or where could i find examples of written code for PIC16f57 that explains every single line of code.
I tried to implement code from examples of other PIC's but didnt worked. I got syntax error and many others.
From what i've found on internet MikroC looked promising but it doesn't support PIC16F57.

__delay_ms doesnt work more than 10 sec.
 

Dodgydave

Joined Jun 22, 2012
11,285
Turn the watchdog timer off WDT=off, if it has an internal Xtal osc use that.

you can use the Tmro and count the rollovers...

What are you trying to achieve with the pic?
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
Check your Private Message (PM).

I can help you with the C Code. I have done such projects like delayed turn ON device, etc...

I will use software timer which will give you precise 30 minutes delay.
 
Last edited:

jayanthd

Joined Jul 4, 2015
945
This is a blocking code. The PIC16F57 doesn't have Interrupt and I was not able to use the Timer0 in Interrupt mode.

According to this code if you press and release button connected to RA0, Relay connected to RC0 will turn ON for 30 minutes and then turns OFF.

C:
#define _XTAL_FREQ 12000000

#include <xc.h>

#pragma config OSC = HS         // Oscillator selection bits (HS oscillator)
#pragma config WDT = ON         // Watchdog timer enable bit (WDT enabled)
#pragma config CP = OFF         // Code protection bit (Code protection off)

#define Button PORTAbits.RA0
#define Relay PORTCbits.RC0

#define ON  1
#define OFF 0

bit run_delay_flag = 0;

void Delay_sec(unsigned int sec);
void Delay_min(unsigned int min);

void Delay_sec(unsigned int sec) {
    unsigned int i;
  
    for(i = 0; i < sec; i++) {
        asm("CLRWDT");
        __delay_ms(200);
        asm("CLRWDT");
        __delay_ms(200);
        asm("CLRWDT");
        __delay_ms(200);
        asm("CLRWDT");
        __delay_ms(200);
        asm("CLRWDT");
        __delay_ms(200);
        asm("CLRWDT");       
    }   
}

void Delay_min(unsigned int min) {
    Delay_sec(min * 60);     
}

void main(void) {
  
    asm("CLRWDT");
  
    TRISA = 0x01;
    TRISB = 0x00;
    TRISC = 0x00;
      
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
  
    asm("CLRWDT");
  
    while(1){
      
        asm("CLRWDT");
      
        if(Button == 0) {
            __delay_ms(50);
            while(Button == 0);
          
            run_delay_flag = 1;
            Relay = ON;           
        }
      
        if(run_delay_flag == 1) {
            Delay_min(30);
            Relay = OFF;
            run_delay_flag = 0;
        }
    }
}
 

dannyf

Joined Sep 13, 2015
2,197
If you were designing a real product, pick a new pic (it is cheaper than f57), and code it like a pro.

If it is indeed a school or hobby project, say so and code accordingly.
 

jayanthd

Joined Jul 4, 2015
945
Go with PIC18F26K22 or PIC18F46K22. They have lots of ROM and RAM. They have sufficient timers for using as a software timer. Additionally you can add a DS1307. If you use all SMD components then board size will be small and also the cost of production.

This code is not tested.

C:
#define _XTAL_FREQ 12000000

#include <xc.h>

#pragma config OSC = HS         // Oscillator selection bits (HS oscillator)
#pragma config WDT = ON         // Watchdog timer enable bit (WDT enabled)
#pragma config CP = OFF         // Code protection bit (Code protection off)

#define Button PORTAbits.RA0
#define Relay PORTCbits.RC0

#define ON  1
#define OFF 0

bit run_delay_flag = 0;

void Delay_mSec(unsigned long int mSec);
void Delay_sec(unsigned long int sec);
void Delay_min(unsigned long int min);

void Delay_mSec(unsigned long int mSec) {
    unsigned int i;
  
    for(i = 0; i < mSec; i++) {
        __delay_ms(1);
        asm("CLRWDT");
      
        if(Button == 0) {
            __delay_ms(50);
            while(Button == 0);
          
            run_delay_flag = 0;
            Relay = OFF;

            break;
        }
    }   
}

void Delay_sec(unsigned long int sec) {
    Delay_mSec(sec * 1000); 
}

void Delay_min(unsigned long int min) {
    Delay_sec(min * 60);     
}

void main(void) {
  
    asm("CLRWDT");
  
    TRISA = 0x01;
    TRISB = 0x00;
    TRISC = 0x00;
      
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
  
    asm("CLRWDT");
  
    while(1){
      
        asm("CLRWDT");
      
        if(Button == 0) {
            __delay_ms(50);
            while(Button == 0);
          
            run_delay_flag = 1;
            Relay = ON;           
        }
      
        if(run_delay_flag == 1) {
            Delay_min(30);
            Relay = OFF;
            run_delay_flag = 0;
        }
    }
}
 
Last edited:

Thread Starter

alex2016

Joined Oct 16, 2016
3
Sorry i've been busy lately and didn't find any time to reply.
I tried the first example and it worked. It was exactly what i was looking for.
Right now i'm gonna keep it at hobby level. I'm really a beginner.
I'm extremely grateful for Jayanthd as he took the time and responded to me very quickly, and provided quality solution.
I have plenty of PIC16F57 which i can break, explode, whatever. Good for a beginner.
I will move towards better pics or microcontrollers, as you suggested, but i just wanted to have a taste of what these things can do with what i had in "my shelf".
Thanks everyone.
 

takao21203

Joined Apr 28, 2012
3,702
the c code mostly is not device specific for trivial tasks.

There is no such thing "this hex file is for 16f628A only".

Even Arduino and PIC is mostly interchangeable, change some IO statements, works.

Learning to program can take years....

Even if you have the use of some hardware device or feature, it could be emulated in software, such as interrupts stacks and timers.
C language does some emulation for instance 16 and 32bit arithmetic.

This is what makes assembler really evil, its tied to a specific chip, even if the chips have similar instructions, the memory addresses and the banking are different. You could extend assembler and use it in some fashion and use your macros and libraries, but then it wont be much different from C, just your own standard (which probably nobody will be able to understand or people will just refuse to look at it).

I had no difficulty taking a C code for a small NOKIA display hook it up to a PIC 16F basically i changed IO statements that was it, worked.

So you could make avail almost all Arduino libraries and sketches, you just have to learn how to isolate things relevant to you.

delay functions are evil dont use them learn programming instead, or, copy&paste 180 times, or, attempt to construct a for loop.

What I suggested to use a DS1307 IC and use the test signal for the timer input, will make your program super simple no need even to read out the DS1307.
 

AlbertHall

Joined Jun 4, 2014
12,345
XC8 includes code for a blocking delay in microseconds and milliseconds:
__delay_us(X)
__delay_ms(X)
You need to put a line up near the top giving the oscillator frequency
#define _XTAL_FREQ 4000000
 
Top