7Segダイナミック接続実験8 PIC16F1827プログラム編
7Segダイナミック点灯実験最後のプログラムです。回路は簡単です。RA0->aRA1->bRA2->cRA3->dRA4->eRA6->fRA7->g(RA5は入力専用です。)RB0->DIG1RB1->DIG2RB2->DIG3RB3->DIG4RB4->DIG5RB5->DIG6RB6->DIG7RB7->DIG8となるように接続してください。利用している7SegがカソードコモンなのでPORTを「0」にして電流を引き込んでいます。トランジスタを利用しませんでしたが、LEDの明るさは変わらない感じで、これで十分な気がします。 ---------------------ここから-----------------------#include <stdio.h>#include <stdlib.h>#include <string.h>#include <xc.h>// CONFIG1#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)#pragma config MCLRE = OFF // MCLR Pin Function Select (MCLR/VPP pin function is digital input)#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)// CONFIG2#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)#pragma config BORV = HI // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.)#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)#define _XTAL_FREQ 32000000#define TMR0CLK 8000000//#define DIGIT 4#define DIGIT 8#if DIGIT == 4#define DIGLMT 0x03#endif#if DIGIT == 8#define DIGLMT 0x07#endif#define ON 1#define OFF 0#define BLANK 0x0f // 7Seg Blank (74HC4511 Decoder)//#define TYPE7447//#define TYPE7448#define TYPE4511#ifdef TYPE7447// Dgfedcbaconst unsigned char tdc[] = { 0b01011111, // 0 0b00000110, // 1 0b10011011, // 2 0b10001111, // 3 0b11000110, // 4 0b11001101, // 5 0b11011100, // 6 0b00000111, // 7 0b11011111, // 8 0b11000111, // 9 0b10011000, // 10 0b10001100, // 11 0b11000010, // 12 0b11001001, // 13 0b11011000, // 14 0b00000000 // 15(BLANK) };#endif#ifdef TYPE7448// Dgfedcbaconst unsigned char tdc[] = { 0b01011111, // 0 0b00000110, // 1 0b10011011, // 2 0b10001111, // 3 0b11000110, // 4 0b11001101, // 5 0b11011101, // 6 0b00000111, // 7 0b11011111, // 8 0b11001111, // 9 0b10011000, // 10 0b10001100, // 11 0b11000010, // 12 0b11001001, // 13 0b11011000, // 14 0b00000000 // 15(BLANK) };#endif#ifdef TYPE4511// Dgfedcbaconst unsigned char tdc[] = { 0b01011111, // 0 0b00000110, // 1 0b10011011, // 2 0b10001111, // 3 0b11000110, // 4 0b11001101, // 5 0b11011100, // 6 0b00000111, // 7 0b11011111, // 8 0b11000111, // 9 0b00000000, // 10 0b00000000, // 11 0b00000000, // 12 0b00000000, // 13 0b00000000, // 14 0b00000000 // 15(BLANK) };#endif#if DIGIT == 4//const unsigned char tdg[] = { ~0b00000001, // DIG1 ~0b00000010, // DIG2 ~0b00000100, // DIG3 ~0b00001000, // DIG4 ~0b00000001, // DIG1 ~0b00000010, // DIG2 ~0b00000100, // DIG3 ~0b00001000, // DIG4 ~0b00000001, // DIG1 ~0b00000010, // DIG2 ~0b00000100, // DIG3 ~0b00001000, // DIG4 ~0b00000001, // DIG1 ~0b00000010, // DIG2 ~0b00000100, // DIG3 ~0b00001000 // DIG4 };#endif#if DIGIT == 8//const unsigned char tdg[] = { ~0b00000001, // DIG1 ~0b00000010, // DIG2 ~0b00000100, // DIG3 ~0b00001000, // DIG4 ~0b00010000, // DIG5 ~0b00100000, // DIG6 ~0b01000000, // DIG7 ~0b10000000, // DIG8 ~0b00000001, // DIG1 ~0b00000010, // DIG2 ~0b00000100, // DIG3 ~0b00001000, // DIG4 ~0b00010000, // DIG5 ~0b00100000, // DIG6 ~0b01000000, // DIG7 ~0b10000000 // DIG8 };#endif// const void *a[] = { &PORTA, &PORTB };union BCD { unsigned char b[DIGIT]; unsigned long d[DIGIT/4];};union BCD lbcd, nbcd;// BCD Data Bufferunsigned char dig; // Digit Counter 0-1-2-3unsigned long tim; // Time(Timer2 Use)/* External */void BINtoBCD(unsigned long, char *, int);void Disp7Seg(void);/* Main */void main(void){ register char i, j; unsigned long dat; // 16F1823 Clock Settiing 32MHz (BaseClock 8MHz + x4 PLL = 32MHz) OSCCON = 0b11110000;// BIT7:1:x4 PLL Enable BIT2:0 BIT1-0:1x:Clock determined by FOSC<2:0> in Configuration Word 1./* BIT6-3 000x = 31 kHz LF 0010 = 31.25 kHz MF 0011 = 31.25 kHz HF(1) 0100 = 62.5 kHz MF 0101 = 125 kHz MF 0110 = 250 kHz MF 0111 = 500 kHz MF (default upon Reset) 1000 = 125 kHz HF(1) 1001 = 250 kHz HF(1) 1010 = 500 kHz HF(1) 1011 = 1MHz HF 1100 = 2MHz HF 1101 = 4MHz HF 1110 = 8 MHz or 32 MHz HF(HFINTOSC x4 PLL) 1111 = 16 MHz HF */ // POART A Initial PORTA = 0b00000000; LATA = 0b00000000; ANSELA = 0b00000000; WPUA = 0b00000000; TRISA = 0b00000000;// RA7-RA0 Output // POART B Initial PORTB = 0b00000000; LATB = 0b00000000; ANSELB = 0b00000000; WPUB = 0b00000000;// 0:Pull-Up disabled TRISB = 0b00000000;// RB7-RB0 Output // Data Initial dig = 0; tim = 0UL; memset((void *)&nbcd, (int)BLANK, sizeof(nbcd));// BCD Buffer Initial Disp7Seg(); // Timer0 Interrupt Enabled // 76543210#if DIGIT == 4 OPTION_REG = 0b00000110;// BIT3:0:Prescaler is assigned to the Timer0 module BIT2-0:Prescaler:128#endif#if DIGIT == 8 OPTION_REG = 0b00000101;// BIT3:0:Prescaler is assigned to the Timer0 module BIT2-0:Prescaler:64#endif TMR0IF = OFF; TMR0IE = ON; // Timer2 Interrupt Enabled // 76543210 0.01sec T2CON = 0b00100111;// BIT6-3:0100:1:5 Postscaler BIT2:1:Timer2 ON BIT1-0:10:Prescaler is 64 TMR2IF = OFF; TMR2IE = ON; // Interrupt Enabled PEIE = ON; GIE = ON; i = 1; j = 0; dat = (unsigned long)i; while(1){ if( tim > 50L ) { tim = 0UL; j++; if( j < DIGIT ) { dat = dat * 10UL + (unsigned long)i; } else { j = 0; i++; if( i > 9 ) { i = 1; } dat = (unsigned long)i; } } BINtoBCD( dat, (char *)&nbcd.d[0], DIGIT ); Disp7Seg(); }}/* Binary to BCD */void BINtoBCD(unsigned long d, char *b, int s){ register char i; unsigned long tmp; for( i = 0; i < s; i++ ) { tmp = d; d /= 10UL; b[i] = (unsigned char)(tmp - (d * 10UL)); } for( i = s - 1; i > 0; i-- ) {// 7Seg BLANK Fill if( b[i] == 0 ) b[i] = BLANK; else break; }}/* 7Seg Display Data Cahnge *//* Data Set Latch Buffer */void Disp7Seg(void){ while( dig );// Digit Display Finish Wait lbcd = nbcd;// To LATCH Buffer Copy}/* Interrupt Routin */static void interrupt interrupt_process(void){ /* 7Seg 1DIGIT Display */ if( TMR0IF ) {// Timer0 Interrupt TMR0IF = OFF; PORTA = 0b00000000; PORTB = tdg[dig]; PORTA = tdc[lbcd.b[dig]]; dig = (( dig + 1 ) & DIGLMT); } /* Clock Count Up */ if( TMR2IF ) {// Timer2 Interrupt TMR2IF = OFF; tim++; }}/* END OF FILE */ ---------------------ここまで----------------------- プログラム的にあまり面白くないです。DefineでロジックICの7447、7448、4511のデコードタイプが選べます。7447と7448では6と9の形が違います。4511は10~14はブランク表示となります。