我正在Proteus中模拟一个项目,我在7段显示器上显示一个三位数(456),但不是显示456,而是显示006......计算完成得很好,但是我不知道问题出在哪里。
这里有图
这里是代码
/* Main.c file generated by New Project wizard
*
* Created: mi. ene. 10 2024
* Processor: PIC16F84A
* Compiler: CCS for PIC
*/
#include <16F84a.h>
#fuses NOWDT
#use delay(clock=4000000)
#define UNO 0x0C // 00001100
#define DOS 0xB6 // 10110110
#define TRES 0x9E // 10011110
#define CUATRO 0xCC // 11001100
#define CINCO 0xDA // 11011010
#define SEIS 0xFA // 11111010
#define SIETE 0x0E // 00001110
#define OCHO 0xFE // 11111110
#define NUEVE 0xCE // 11001110
#define CERO 0x7E // 01111110
#define ERROR 0b11110010
#define U 0x1
#define D 0x2
#define C 0x4
byte mostrar(int index){
switch(index) {
case 0: return CERO;
case 1: return UNO;
case 2: return DOS;
case 3: return TRES;
case 4: return CUATRO;
case 5: return CINCO;
case 6: return SEIS;
case 7: return SIETE;
case 8: return OCHO;
case 9: return NUEVE;
default: return ERROR; // Manejar casos no definidos
}
}
int main (void)
{
set_tris_a(0x00); // Configura todos los pines del Puerto A como salida
set_tris_b(0x01); // Configura RB0 como entrada, RB1-RB7 como salida
while(true){
int numero = 456;
int unidad = numero % 10;
int decena = (numero / 10) % 10;
int centena = numero / 100;
// unidad
output_a(U);
output_b(mostrar(unidad));
delay_ms(10);
output_a(0);
output_a(D);
output_b(mostrar(decena));
delay_ms(10);
output_a(0);
output_a(C);
output_b(mostrar(centena));
delay_ms(10);
output_a(0);
}
}
我希望能够在各自的显示器上很好地展示数字456,然后赋予它不同的用途。
/* Main.c file generated by New Project wizard
*
* Created: mi. ene. 10 2024
* Processor: PIC16F84A
* Compiler: CCS for PIC
*/
#include <16F84A.h>
#use delay(clock=4000000,crystal)//Crystal Externo 4MHz
#FUSES XT,NOWDT
Byte CONST display[10]= {0x7E,0x0C,0xB6,0x9E,0xCC,0xDA,0xFA,0x0E,0xFE,0xCE};
#BYTE PORTB= 6
#define U 0x1
#define D 0x2
#define C 0x4
int16 numero;
int unidad,decena,centena;
//#define ERROR 0b11110010
int main (void)
{
set_tris_a(0b11111000); // Configura todos los pines del Puerto A como salida
set_tris_b(0x01); // Configura RB0 como entrada, RB1-RB7 como salida
while(true){
numero=456;
unidad = numero % 10;
decena = (numero / 10) % 10;
centena = numero / 100;
//unidad
output_a(U);
PORTB = (display[unidad]);
delay_ms(10);
output_a(0);
//decena
output_a(D);
PORTB = (display[decena]);
delay_ms(10);
output_a(0);
//centena
output_a(C);
PORTB = (display[centena]);
delay_ms(10);
output_a(0);
}
}
从 main 开始声明
numero
,如 int16
、unidad
、decena
和 centena
,如 int
!
这里是使用中断来刷新 LED 数字的实现:
/*
* File: main.c
* Author: dan1138
* Target: PIC16F84A
* Compiler: XC8 v2.45
* IDE: MPLABX v6.15
*
* Created on January 19, 2024, 11:42 AM
*/
/* Define the system oscillator this code will setup */
#define _XTAL_FREQ (4000000ul)
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (Power-up Timer is enabled)
#pragma config CP = OFF // Code Protection bit (Code protection disabled)
#include <xc.h>
#include <stdint.h>
const char LEDDigit[] =
{
/*gfedcba _ */
0b01111110, /* | | */
/* |_| */
/* */
0b00001100, /* | */
/* | */
/* _ */
0b10110110, /* _| */
/* |_ */
/* _ */
0b10011110, /* _| */
/* _| */
/* */
0b11001100, /* |_| */
/* | */
/* _ */
0b11011010, /* |_ */
/* _| */
/* _ */
0b11111010, /* |_ */
/* |_| */
/* _ */
0b00001110, /* | */
/* | */
/* _ */
0b11111110, /* |_| */
/* |_| */
/* _ */
0b11001110, /* |_| */
/* | */
/* _ */
0b11101110, /* |_| */
/* | | */
/* */
0b11111000, /* |_ */
/* |_| */
/* _ */
0b01110010, /* | */
/* |_ */
/* */
0b10111100, /* _| */
/* |_| */
/* _ */
0b11110010, /* |_ */
/* |_ */
/* _ */
0b11100010, /* |_ */
/* | */
/* */
0b00000000, /* blank */
/* */
};
volatile uint8_t Digit1Segments;
volatile uint8_t Digit2Segments;
volatile uint8_t Digit3Segments;
void __interrupt() ISR_handler(void)
{
static uint8_t Timer0Ticks;
if (TMR0IE && TMR0IF) { /* TIMER0 asserts and interrupt every 1.024 milliseconds */
TMR0IF=0;
if ((Timer0Ticks++ & 0x0F) == 0) { /* every 16.384ms drive a new digit */
PORTB &= ~0b11111110; /* Turn off all segment drivers */
if ((TRISA & 7) == 0b011) {
TRISA |= 0b111;
TRISA &= 0b110; /* Drive digit 1 */
PORTB |= Digit1Segments;
}
else if ((TRISA & 7) == 0b110) {
TRISA |= 0b111;
TRISA &= 0b101; /* Drive digit 2 */
PORTB |= Digit2Segments;
}
else {
TRISA |= 0b111;
TRISA &= 0b011; /* Drive digit 3 */
PORTB |= Digit3Segments;
}
}
}
}
void main(void) {
uint16_t Count;
/*
* Initialize this PIC
*/
INTCON = 0;
__delay_ms(500); /* Give ICSP device programming tool a chance to get the PICs attention */
Digit1Segments = 0;
Digit2Segments = 0;
Digit3Segments = 0;
TRISA = 0xFF; /* PORTA bits 0,1,2 are digit drivers */
TRISB = 0x01; /* PORTB bits 7 to 1 are segment drivers */
OPTION_REG = 0b11000001; /* TIMER0 clock = FOSC/4, prescale 1:4 */
PORTA = 0;
PORTB = 0;
TMR0 = 0;
TMR0IF = 0;
TMR0IE = 1;
GIE = 1;
/*
* This is the application loop.
*
* It counts up one count about every second.
*/
Count = 0;
while(1)
{
Digit1Segments = LEDDigit[(Count ) % 10];
Digit2Segments = LEDDigit[(Count / 10) % 10];
Digit3Segments = LEDDigit[(Count / 100) % 10];
__delay_ms(1000);
Count++;
}
}