0-999带有PIC16F877A的上下计数器

问题描述 投票:0回答:1

我对微处理器C编程有问题。

Hardware picture is here!

我必须在C编译器上编写代码,对于给定的硬件,计算D端口上的10 ^ 0位,C端口上的10 ^ 1位和B端口上的10 ^ 2位。同样,通过pin_a0进行计数的方向也受到限制。如果pin_a0 = 1,硬件将向前计数,否则,向后计数。

非常感谢您的帮助,非常感谢

pic microprocessors proteus
1个回答
0
投票

首先,您提出问题的方式确实不符合本论坛的准则。

第二,我不太在乎指南。

第三,我正在为您的家庭作业发布一种可能的解决方案:

/*
 * File:   main.c
 * Author: dan1138
 *
 * Created   on May 3, 2020, 4:08 PM
 * Completed on May 3, 2020, 4:43 PM
 * Simulated on May 3, 2020, 4:55 PM
 */

// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdint.h>

uint8_t  Digits[3];

void ZeroDigits( void )
{
    uint8_t i;

    for(i = 0; i < sizeof(Digits); i++)
    {
        Digits[i] = 0;
    }
}

void CountDigitsUp( void )
{
    Digits[0]++;
    if(Digits[0] > 9)
    {
        Digits[0] = 0;
        Digits[1]++;
        if(Digits[1] > 9)
        {
            Digits[1] = 0;
            Digits[2]++;
            if(Digits[2] > 9)
            {
                Digits[2] = 0;
            }
        }
    }
}

void CountDigitsDown( void )
{
    Digits[0]--;
    if(Digits[0] > 9)
    {
        Digits[0] = 9;
        Digits[1]--;
        if(Digits[1] > 9)
        {
            Digits[1] = 9;
            Digits[2]--;
            if(Digits[2] > 9)
            {
                Digits[2] = 9;
            }
        }
    }
}

void ShowDigits( void )
{
    PORTD = Digits[0]<<4;
    PORTC = Digits[1]<<2;
    PORTB = Digits[2];
}

void main(void) 
{
    uint16_t Ticks;

    INTCON = 0;
    PIE1   = 0;
    PIE2   = 0;

    OPTION_REG = 0b11000001;

    TRISA  =  0x03;
    TRISB  =  0x00;
    TRISC  =  0x00;
    TRISD  =  0x00;
    TRISE &= ~0x07; 

    /* Make all GPIO pins digital */
    CMCON  = 0x07;
    ADCON1 = 0x06;

    Ticks = 0;
    ZeroDigits();

    for(;;)
    {
        ShowDigits();
        if(PORTAbits.RA0 == 1)
        {
            CountDigitsUp();
        }
        else
        {
            CountDigitsDown();
        }
    }
}

现在我确实遗漏了您需要提供的所有重要评论,才能将其作为完成的作业上交。

© www.soinside.com 2019 - 2024. All rights reserved.