Skip to content

Instantly share code, notes, and snippets.

@JulioAlexanderAguilarAngulo
Last active August 29, 2015 14:25
Show Gist options
  • Save JulioAlexanderAguilarAngulo/4304e6531bb80e077799 to your computer and use it in GitHub Desktop.
Save JulioAlexanderAguilarAngulo/4304e6531bb80e077799 to your computer and use it in GitHub Desktop.
/* Main.c file generated by New Project wizard
* Author: Julio Alexander Aguilar Angulo.
* [email protected]
* alliselectronics.tumblr.com
* Created: jeu. juil. 23 2015
* Processor: PIC16F648A
* Compiler: MPLAB XC8
*/
#include <xc.h>
#include <stdint.h>
#define CommonCathode
#include <pic16f648a.h>
// fuse configuration
#pragma config CP=OFF, CPD=OFF, BOREN=OFF, WDTE=OFF//SWDTEN
#pragma config PWRTE=ON, FOSC=INTOSCIO, MCLRE=OFF
#pragma config LVP=OFF
// diverse compiler's parameters
#define _XTAL_FREQ 4000000 // 4MHz
#define FOSC 4000000 // 4MHz
#define DAT_4094 1<<4
#define CLK_4094 1<<5
#define ENA_4094 1<<6
#define STB_4094 1<<7
// 7 segments = gfedcba
uint8_t segs[]={0b0111111,0b0000110,0b1011011,0b1001111,0b1100110,0b1101101,0b1111101,0b0000111, 0b1111111,0b1101111};
uint8_t sels[]={0b000000111,0b00001011,0b00001101,0b00001110};
uint16_t par[]={1,10,100,1000};
uint8_t buffer_mcdu[]={0,0,0,0};
uint8_t i,j,it;
uint16_t counter;
void Config_GPIOs(void);
void Delay1(void);
void Convert(uint16_t counter);
void interrupt TIM0int(void);
void ConfigInterrupts(void);
void SendData_4094(uint8_t sel,uint8_t data);
// Interrupt Handler
void interrupt TIM0int(void)
{
if (INTCONbits.TMR0IF ==1) // timer 0 interrupt flag
{
INTCONbits.TMR0IF = 0; // clear the flag
INTCONbits.TMR0IE = 1; // reenable the interrupt
TMR0 = 3; // 6 reset the timer preset count
SendData_4094(sels[i],segs[buffer_mcdu[i]]);
i++;
if (i==4)
{
i=0;j++;
if (j==50)
{ j=0;counter++; Convert(counter); }
}
}
}
void main()
{ Config_GPIOs();
ConfigInterrupts();
i=0;j=0;
counter=0;
while (1);
}
void Config_GPIOs(void)
{ CMCON=0x07; TRISA= ~( STB_4094 | ENA_4094 | 0x0F); TRISB= ~( DAT_4094 | CLK_4094 ); PORTA=0x00; PORTB=0x00; }
void Delay1(void)
{ uint16_t tmp; tmp=800; while (tmp>0){tmp--;} }
void Convert(uint16_t Val)
{
uint16_t Valt,part;
Valt=Val;
for(it=3;it>0;it--) { part=par[it]; buffer_mcdu[it]=Valt/part; Valt %=part; }
buffer_mcdu[0]=Valt;
}
void ConfigInterrupts(void)
{
OPTION_REGbits.T0CS = 0; // bit 5 TMR0 Clock Source Select bit...0 = Internal Clock (CLKO) 1 = Transition on T0CKI pin
OPTION_REGbits.T0SE = 0; // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low
OPTION_REGbits.PSA = 0; // bit 3 Prescaler Assignment bit...0 = Prescaler is assigned to the Timer0
OPTION_REGbits.PS2 = 0; // bits 2-0 PS2:PS0: Prescaler Rate Select bits
OPTION_REGbits.PS1 = 0;//1
OPTION_REGbits.PS0 = 1;
TMR0 = 3; // preset for timer register
// Interrupt Registers
INTCON = 0; INTCONbits.TMR0IE = 1; INTCONbits.TMR0IF = 0; INTCONbits.GIE = 1;
}
void SendData_4094(uint8_t sel,uint8_t data)
{
uint8_t LOOP, FLAG;
PORTA &= ~ ( ENA_4094 | STB_4094 ) ; PORTB &= ~ ( CLK_4094 ); PORTA |= ENA_4094;
for (LOOP=0; LOOP<8; LOOP++) { PORTB &= ~ ( CLK_4094 ); FLAG=sel&0x80;
if(FLAG==0){ PORTB &= ~ ( DAT_4094 );} else { PORTB |= DAT_4094 ;}
sel <<= 1; PORTB |= ( CLK_4094 ); }
for (LOOP=0; LOOP<8; LOOP++) { PORTB &= ~ ( CLK_4094 ); FLAG=data&0x80;
if(FLAG==0){ PORTB &= ~ ( DAT_4094 );} else { PORTB |= DAT_4094 ;}
data <<= 1; PORTB |= ( CLK_4094 ); }
PORTB &= ~ ( CLK_4094 ); PORTA |= STB_4094;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment