わんこ

わんこ

Xmas Tree for PIC16F628



#include <htc.h>

//-----------------------------------------------------------------------
// global define
//-----------------------------------------------------------------------
#define PA_INIT 0b00100000 //porta ra5(mcr) : in
#define PB_INIT 0b00000000 //portb initial
#define PAD_INIT 0b00000000 //porta data
#define PBD_INIT 0b11111111 //portb data
#define CMCON_INIT 0b00000111 //ra3:0 digital i/o
#define TMR0_INIT 0b10000000 //timer0 initial
#define INT_INIT 0b10100000 //intcon initial
//-----------------------------------------------------------------------
#define DMASK 0x000f //dcode mask code
#define TMASK 0x00f0 //time  mask code
#define COM_SEL 0x0010 //COM0/1 select bit
#define COM0_ON 0b01000001 //COM0,COM0A
#define COM1_ON 0b10000010 //COM1,COM1A
//-----------------------------------------------------------------------
int TBASE=0, //base timer+0/+1
CNT=0; //advance timer+0/+1
unsigned char VRAM[4], //display ram (8bit x 2) x 2
DISP, //display ram (8bit x 1) buffer
CTIME=0; //current time(0 to 15)4bit assign
//-----------------------------------------------------------------------
//----- regster initial routine -----
//-----------------------------------------------------------------------
void initialize() //port/reg initialize
{
//----- port init -----
PORTA= PAD_INIT; //COM OFF
PORTB= PBD_INIT; //LED OFF
//
TRISA= PA_INIT; //PORTA DIR
TRISB= PB_INIT; //PORTB DIR
//----- timer0 init -----
OPTION_REG= TMR0_INIT;
CMCON= CMCON_INIT;
//----- vram init -----
VRAM[0]=0;  VRAM[1]=0;
VRAM[2]=0;  VRAM[3]=0;
DISP=0;
//----- interrupt init -----
INTCON= INT_INIT;
}
//-----------------------------------------------------------------------
//----- display data convert -----
//-----------------------------------------------------------------------
gen_disp(char *vram_data)
{
if((vram_data[1] & DMASK) > CTIME){
    DISP=0; // time is eary
}
else{
    DISP=vram_data[0];
}
}
//-----------------------------------------------------------------------
// interrupt routine (timer0)
//-----------------------------------------------------------------------
void interrupt tc_int(void)
{
if (T0IE && T0IF){
    T0IF=0; //clear int req
    if(CNT) CNT--; //interval timer count
    TBASE++; //led colation counter
    CTIME=(unsigned char)(TBASE & DMASK);
    if(CTIME){
if(TBASE & COM_SEL){ //COM1 set
    PORTA=COM1_ON;
    gen_disp(VRAM+2);
    PORTB=~DISP; //invert
}
else{ //COM0 set
    PORTA=COM0_ON;
    gen_disp(VRAM+0);
    PORTB=~DISP; //invert
}
    }
    else{
PORTA=PAD_INIT; //COM ALL OFF
PORTB=PBD_INIT; //LED ALL OFF
    }
}
}
//-----------------------------------------------------------------------
// main routine
//-----------------------------------------------------------------------
void main(void)
{
// pic16f628 configration code
__CONFIG(CP_OFF & CPD_OFF & LVP_OFF & BOREN_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTOSCCLK);

int table_adr=0; //led table address
unsigned char led_on_time; //LED ON start time(1 to 15)4bit assign
//-----------------------------------------------------------------------
// LED TABLE DATA
//-----------------------------------------------------------------------
#include "table_data.inc";
//-----------------------------------------------------------------------
initialize(); //port/reg initialize
//
T0IF=0;
T0IE=1; // timer0 interrupt enable set
ei();
//-----------------------------------------------------------------------
while(1){
//LED lighting data set 
VRAM[0]=table_data[table_adr++]; //blue led data read 1/2
VRAM[1]=table_data[table_adr++]; //blue led data read 2/2
VRAM[2]=table_data[table_adr++]; //red  led data read 1/2
VRAM[3]=table_data[table_adr++]; //red  led data read 2/2
led_on_time=VRAM[3] & TMASK;
if(led_on_time==0) table_adr=0; // check table data end or not

di();
CNT=(int)(led_on_time<<3); // watie time set
ei();
while(CNT); // wait till CNT == 0
}
}

・ROMの点灯パターン"table_data.inc"


const unsigned char table_data[]={

0b11111111, // com0 LED 8bit data VRAM[0]
1, // com0 LED brite level 1(max) to 15(min)
0b11111111, // com1 LED 8bit data
3*16 + 1, // LED brite time + com1 LED brite level 1(max) to 15(min)

0b11111111,
2,
0b11111111,
3*16 + 2,

0b11111111,
3,
0b11111111,
3*16 + 3,

0b11111111,
4,
0b11111111,
3*16 + 4,
    .
    .
    .

0b00000000,
1,
0b10000000,
4*16 + 1,


0b00000000,
1,
0b00000000,
12*16 + 1,


0b00000000,
15,
0b00000000,
0*16 + 15 // table end code LED brite time=0
};

© Rakuten Group, Inc.
X
Design a Mobile Site
スマートフォン版を閲覧 | PC版を閲覧
Share by: