Wednesday, April 5, 2017

Programa de prueba para PIC16F628A.

Hola, en este nuevo post os voy a poner un programa de ejemplo para usar con el medidor de frecuencia con PIC16F628A. El entorno de desarrollo que uso es el MPLAB X IDE con compilador XC8.


En primer lugar, aquí podeis descargar el firmware original, para poder restaurar el medidor de frecuencia a su estado original. 

El programa que podeis usar para probar si el medidor se puede reprogramar es muy sencillo, va mostrando numeros en los displays de 7 segmentos. El programa podeis descargarlo aquí.

Si teneis alguna pregunta, no dudeis en ponerla en los comentarios. Hasta la próxima, saludos.



/* 
 * File:   Test_Freq_Counter.c
 * Author: Lenovo_17
 *
 * Created on 1. April 2017, 21:11
 */

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#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)

#include <stdio.h>
#include <stdlib.h>
#include <xc.h>

#define _XTAL_FREQ 20000000


/*RB0 d
  RB1 DP
  RB2 c
  RB3 e
  RB4 f
  RB5 g
  RB6 a
  RB7 b*/

/*RA0 3
  RA1 4
  RA2 2
  RA3 1*/

//PORTB
//#define PIN_A RB6
//#define PIN_B RB7
//#define PIN_C RB2
//#define PIN_D RB0
//#define PIN_E RB3
//#define PIN_F RB4
//#define PIN_G RB5
//#define PIN_DP RB1

#define BIT_A 1 << 6
#define BIT_B 1 << 7
#define BIT_C 1 << 2
#define BIT_D 1 << 0
#define BIT_E 1 << 3
#define BIT_F 1 << 4
#define BIT_G 1 << 5
#define BIT_DP 1 << 1


//#define PIN_D1 RA3
//#define PIN_D2 RA2
//#define PIN_D3 RA0
//#define PIN_D4 RA1

//PORTA 
#define D1 0b00000111;
#define D2 0b00001011;
#define D3 0b00001110;
#define D4 0b00001101;
#define D5 0b00001111;


unsigned char num2seg(unsigned char num){
    unsigned char seg = 0;
    switch(num){
        case 0: seg = BIT_A | BIT_B | BIT_C | BIT_D | BIT_E | BIT_F; break;
        case 1: seg = BIT_B | BIT_C; break;
        case 2: seg = BIT_A | BIT_B | BIT_G | BIT_D | BIT_E; break;
        case 3: seg = BIT_A | BIT_B | BIT_C | BIT_D | BIT_G; break;
        case 4: seg = BIT_B | BIT_C | BIT_G | BIT_F; break;
        case 5: seg = BIT_A | BIT_C | BIT_D | BIT_F | BIT_G; break;
        case 6: seg = BIT_A | BIT_C | BIT_D | BIT_E | BIT_F | BIT_G; break;
        case 7: seg = BIT_A | BIT_B | BIT_C; break;
        case 8: seg = BIT_A | BIT_B | BIT_C | BIT_D | BIT_E | BIT_F | BIT_G; break;
        case 9: seg = BIT_A | BIT_B | BIT_C | BIT_D | BIT_F | BIT_G; break;
        default: seg = BIT_A | BIT_B | BIT_C | BIT_D | BIT_E | BIT_F | BIT_G | BIT_DP; break;
    }
    return seg;
}


void init_ports(void){
    //PINS 12f675 GP0 and GP1 LEDS, Output. GP2 and GP4 keys, input. GP3 Master clear reset. GP4 ADC 
    //ANSEL = 0x58;       // 0b 0101 1000  16TOSC 101 ANS3 SET Analog 0x58 //8Tosc = 001   0x18
    //ADCON0 |= 0x8C;      // 0b 1000 1100   ADFM 1 (right justified ) //VCFG 0 (VDD) //  11 = Channel 03 (AN3 //GP4)  10xx 1100 last 2 bits to start ad conversion
    //ADCON0 = 0b10001101;    //ADON 1, GODone 0
    CMCON = 0x07;
    VRCON = 0x00;
    TRISA &= ~0x0f;  //after reset, trisio register reads all bits as 1
    TRISB = 0x00;
    PORTA |= 0x0f;
    PORTB = 0x00;
    //GPIO = 0x00;    // 0b xxxx xx11     //turn leds off if high 
    
//    INTE = 1;       //activate interrupt in pin 2
//    INTEDG = 0;     //interrupt on falling edge pin 2
//    GIE = 1;        //enable global interrupts
}





//long map(unsigned int x, unsigned int in_min, unsigned int in_max, unsigned int out_min, unsigned int out_max)
//{
//  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
//}

/*
 * 
 */
int main(int argc, char** argv) {
    __delay_ms(1500);   //wait a bit to allow the chip to be reprogrammed. important if you make gp0 and gp1 outputs (pins to programm pic)
    init_ports();
    unsigned char digits[5];// = {D1,D2,D3,D4,D5};
    digits[0] = D1;
    digits[1] = D2;
    digits[2] = D3;
    digits[3] = D4;
    digits[4] = D5;
    unsigned char values[5]= {9,0,1,2,3};
    unsigned long count;
    while(1){
        for(unsigned char j=0;j<10;j++){
            values[0] += 1;
            values[1] += 1;
            values[2] += 1;
            values[3] += 1;
            values[4] += 1;
            count = 0;
            while(count < 30){
            for(unsigned char i = 1; i <= 5  ;i++){
                PORTB = num2seg(values[i-1] % 10);
                PORTA = digits[i-1];
                __delay_ms(4); //50 20 10 5
            }
            count++;
        /*
        for(unsigned char i = 0; i<=9; i++){
        PORTB = num2seg(i);
        PORTA = D1;
        __delay_ms(200);
        PORTA = D2;
        __delay_ms(200);
        PORTA = D3;
        __delay_ms(200);
        PORTA = D4;
        __delay_ms(200);
        PORTA = D5;
        __delay_ms(200);
          */
            }
        }
    }
    return (EXIT_SUCCESS);
}

No comments:

Post a Comment