Need help with PIC 16F57

Discussion in 'Embedded Systems and Microcontrollers' started by alex2016, Oct 16, 2016.

  1. alex2016

    Thread Starter New Member

    Oct 16, 2016
    3
    0
    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 (Text):
    1. #pragma config OSC = HS         // Oscillator selection bits (HS oscillator)
    2. #pragma config WDT = ON         // Watchdog timer enable bit (WDT enabled)
    3. #pragma config CP = OFF         // Code protection bit (Code protection off)
    4.  
    5. // #pragma config statements should precede project file includes.
    6. // Use project enums instead of #define for ON and OFF.
    7.  
    8. #include <xc.h>
    9.  
    10. #define _XTAL_FREQ 12000000
    11.  
    12. void main(void) {
    13.     TRISB = 0x00;
    14.         while(1){
    15.         PORTB = 0xFF;
    16.         __delay_ms(2000);
    17.         PORTB = 0x00;
    18.         __delay_ms(2000);
    19.      
    20.     }
    21. }
    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.
     
  2. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  3. alex2016

    Thread Starter New Member

    Oct 16, 2016
    3
    0
    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.
     
  4. Dodgydave

    Distinguished Member

    Jun 22, 2012
    4,969
    744
    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: Oct 17, 2016
  5. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,796
    831
    Wait for button press
    Set output high
    So count 180 occurrences of 10 seconds.
    Set output low
    Lather, rinse and repeat
     
  6. jayanthd

    Member

    Jul 4, 2015
    265
    27
    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: Oct 17, 2016
  7. jayanthd

    Member

    Jul 4, 2015
    265
    27
    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.

    Code (C):
    1.  
    2. #define _XTAL_FREQ 12000000
    3.  
    4. #include <xc.h>
    5.  
    6. #pragma config OSC = HS         // Oscillator selection bits (HS oscillator)
    7. #pragma config WDT = ON         // Watchdog timer enable bit (WDT enabled)
    8. #pragma config CP = OFF         // Code protection bit (Code protection off)
    9.  
    10. #define Button PORTAbits.RA0
    11. #define Relay PORTCbits.RC0
    12.  
    13. #define ON  1
    14. #define OFF 0
    15.  
    16. bit run_delay_flag = 0;
    17.  
    18. void Delay_sec(unsigned int sec);
    19. void Delay_min(unsigned int min);
    20.  
    21. void Delay_sec(unsigned int sec) {
    22.     unsigned int i;
    23.  
    24.     for(i = 0; i < sec; i++) {
    25.         asm("CLRWDT");
    26.         __delay_ms(200);
    27.         asm("CLRWDT");
    28.         __delay_ms(200);
    29.         asm("CLRWDT");
    30.         __delay_ms(200);
    31.         asm("CLRWDT");
    32.         __delay_ms(200);
    33.         asm("CLRWDT");
    34.         __delay_ms(200);
    35.         asm("CLRWDT");      
    36.     }  
    37. }
    38.  
    39. void Delay_min(unsigned int min) {
    40.     Delay_sec(min * 60);    
    41. }
    42.  
    43. void main(void) {
    44.  
    45.     asm("CLRWDT");
    46.  
    47.     TRISA = 0x01;
    48.     TRISB = 0x00;
    49.     TRISC = 0x00;
    50.      
    51.     PORTA = 0x00;
    52.     PORTB = 0x00;
    53.     PORTC = 0x00;
    54.  
    55.     asm("CLRWDT");
    56.  
    57.     while(1){
    58.      
    59.         asm("CLRWDT");
    60.      
    61.         if(Button == 0) {
    62.             __delay_ms(50);
    63.             while(Button == 0);
    64.          
    65.             run_delay_flag = 1;
    66.             Relay = ON;          
    67.         }
    68.      
    69.         if(run_delay_flag == 1) {
    70.             Delay_min(30);
    71.             Relay = OFF;
    72.             run_delay_flag = 0;
    73.         }
    74.     }
    75. }
    76.  
     
  8. dannyf

    Well-Known Member

    Sep 13, 2015
    1,771
    358
    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.
     
  9. jayanthd

    Member

    Jul 4, 2015
    265
    27
    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.

    Code (C):
    1.  
    2. #define _XTAL_FREQ 12000000
    3.  
    4. #include <xc.h>
    5.  
    6. #pragma config OSC = HS         // Oscillator selection bits (HS oscillator)
    7. #pragma config WDT = ON         // Watchdog timer enable bit (WDT enabled)
    8. #pragma config CP = OFF         // Code protection bit (Code protection off)
    9.  
    10. #define Button PORTAbits.RA0
    11. #define Relay PORTCbits.RC0
    12.  
    13. #define ON  1
    14. #define OFF 0
    15.  
    16. bit run_delay_flag = 0;
    17.  
    18. void Delay_mSec(unsigned long int mSec);
    19. void Delay_sec(unsigned long int sec);
    20. void Delay_min(unsigned long int min);
    21.  
    22. void Delay_mSec(unsigned long int mSec) {
    23.     unsigned int i;
    24.  
    25.     for(i = 0; i < mSec; i++) {
    26.         __delay_ms(1);
    27.         asm("CLRWDT");
    28.      
    29.         if(Button == 0) {
    30.             __delay_ms(50);
    31.             while(Button == 0);
    32.          
    33.             run_delay_flag = 0;
    34.             Relay = OFF;
    35.  
    36.             break;
    37.         }
    38.     }  
    39. }
    40.  
    41. void Delay_sec(unsigned long int sec) {
    42.     Delay_mSec(sec * 1000);
    43. }
    44.  
    45. void Delay_min(unsigned long int min) {
    46.     Delay_sec(min * 60);    
    47. }
    48.  
    49. void main(void) {
    50.  
    51.     asm("CLRWDT");
    52.  
    53.     TRISA = 0x01;
    54.     TRISB = 0x00;
    55.     TRISC = 0x00;
    56.      
    57.     PORTA = 0x00;
    58.     PORTB = 0x00;
    59.     PORTC = 0x00;
    60.  
    61.     asm("CLRWDT");
    62.  
    63.     while(1){
    64.      
    65.         asm("CLRWDT");
    66.      
    67.         if(Button == 0) {
    68.             __delay_ms(50);
    69.             while(Button == 0);
    70.          
    71.             run_delay_flag = 1;
    72.             Relay = ON;          
    73.         }
    74.      
    75.         if(run_delay_flag == 1) {
    76.             Delay_min(30);
    77.             Relay = OFF;
    78.             run_delay_flag = 0;
    79.         }
    80.     }
    81. }
    82.  
     
    Last edited: Oct 17, 2016
  10. alex2016

    Thread Starter New Member

    Oct 16, 2016
    3
    0
    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.
     
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  12. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,889
    375
    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
     
Loading...