多个LED和使用和输入按钮

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

对于下面的问题1,我真的被困在a和b部分。我真的很困惑如何改变乘法和除法功能来改变引脚/ LED,改为使用<<和>>函数。任何帮助将非常感激。谢谢!

多个LED和使用和输入按钮

  1. 修改C程序:

一个。不使用乘法和除法功能来改变引脚/ LED,而是使用<<和>>功能。参考文献:Deitel和Deitel“C,如何编程和https://en.wikipedia.org/wiki/Operators_in_C_and_C

湾将程序中的时钟频率更改为1 MHz,并使每个LED的开/关时间为.1秒。这应该使旋转明显更快。 (请记住更改_XTAL_FREQ值,因为它用于XC8中内置的__delay_ms()函数)

设备:

低引脚数板(板载16F1829)和44引脚演示板都在同一背板上。 (您只在本实验中使用16F1829。)

PICKIT 3编程器带USB线

MPLAB X(我使用的是v3.00,但实验室计算机上可能有不同的版本))

Microchip XC8 C编译器用户手册

PIC16F1829数据手册

PICkit 3用户指南

低引脚数板用户指南

“C如何编程”Deitel,Pearson / Prentice-Hall(任何版本)

用于研究的互联网浏览器搜索引擎(谷歌,必应等)upload_2018-9-5_23-27-22.png

代码如下。

/*
LEDs on for approximately 0.5 sec.
PIC: 16F1829 Enhanced Mid-Level
Compiler: XC8 v1.34
IDE: MPLABX v3.00 */

#include <pic16f1829.h> //Not required but this is the reference used by "C" for names and location on uC
#include <htc.h> //refers on HiTech C, Microchip purchased HiTech
#define _XTAL_FREQ 4000000 //Used by the XC8 delay_ms(x) macro
#define switch PORTAbits.RA2 // Can use RA2 instead of PORTAbit.RA2 to define pin attached to switch

//instead of saying PORTAbits.RA2 each time
//config bits for the PIC16F1829
#pragma config FOSC=INTOSC, WDTE=OFF, PWRTE=OFF, MCLRE=OFF, CP=OFF, CPD=OFF, BOREN=ON, CLKOUTEN=OFF, IESO=OFF, FCMEN=OFF
#pragma config WRT=OFF, PLLEN=OFF, STVREN=OFF, LVP=OFF

//Initialization subroutine
void initialize(void) {
    ANSELC=0; //All pins of Port C are digital I/O
    ANSA2=0; //switch pin, RA2, is digital IO
    TRISA2 = 1; //switch is an input
    TRISC = 0; //all pins of Port C are outputs
    OSCCON = 0b01101000; // 4 MHz
}

unsigned char i1; //only need 4 bits to count to 16. unsigned character variable is 8 bits long

// Here is main(). There are many ways to do this 4-pin (LED) sequence
void main(void)
{
    initialize();
    i1=1; //Start the main program with the variable =1. Could have done this during its definition

    while (1) //runs continuously until MCU is shut off
    {

        if (switch==1) //Button not pressed pin at 5V
        { i1=1; }
        while (switch==1) //Button not pressed
        {
            PORTC=i1; //Note that writing to PORTC writes to LATC
            __delay_ms(500);
            i1=i1*2;
            if (i1==16)
            { i1=1; }
        }

        if (switch==0) //Button pressed pin at ground
        { i1=8; }
        while (switch==0) //Button pressed
        {
            PORTC=i1;
            __delay_ms(500);
            i1=i1/2;
            if (i1==0)
            { i1=8; }
        }
    }
}  
c microprocessors
1个回答
1
投票

一个。不使用乘法和除法功能来改变引脚/ LED,而是使用<<和>>功能。参考文献:Deitel和Deitel“C,如何编程和https://en.wikipedia.org/wiki/Operators_in_C_and_C

左移value << n是值的整数乘以2 ^ n或值*(2 ^ n)

右移value >> n是整数除值2 ^ n或值/(2 ^ n))

当你有一些var并使用其中一个移位运算符时,你将获取var的值,并将表示它的值的二进制数字(位)向左或向右移动。

一个基本的例子:

uint8_t var = 1; //0b00000001 in binary
var <<= 1; //var is now 0b00000010, that is 1*(2^1) or 2
var >>= 1; //var is now 0b00000001, that is 2/(2^1) or 1

使用移位运算符有一个很大的警告,那就是每当你移位时你从正在移动的相反方向填充0,所以你必须注意整数大小。

uint8_t var = 1;
var <<= 4; //var is now 0b00010000, 4 zeros filled in on the right
var = 1;
var <<= 8; //var is now 0b00000000, because 8 zeros were filled in on the right!

现在关于如何使用它来操作微控制器上的引脚,你可以采用一些递增或递减的变量,并通过该变量向左或向右移动,并将结果值分配给控制该引脚的模块中的寄存器,在这种情况下是PORTx模块。在你的代码中看起来像这样:

    if (switch == 1) //Button not pressed pin at 5V
    {

        i1 = 0; //initialize to 0

    }
    while (switch == 1) //Button not pressed
    {

        PORTC = (1 << i1++); //set will set just one pin at a time, the first will be pin 0, the next pin 1, and so on

        __delay_ms(500);

        if (i1 == 8){

            i1 = 0; //reset variable

        }

    }

    if (switch == 0) //Button pressed pin at ground
    {

        i1 = 0; //initialize to 0

    }
    while (switch == 0) //Button pressed
    {
        PORTC = (0x80 >> i1++); //this will set 1 pin at a time, the first will be pin 7, the next will be pin 6, and so on

        __delay_ms(500);

        if (i1 == 8)
        {
            i1 = 0; //reset variable
        }

    }

湾将程序中的时钟频率更改为1 MHz,并使每个LED的开/关时间为.1秒。这应该使旋转明显更快。 (请记住更改_XTAL_FREQ值,因为它用于XC8中内置的__delay_ms()函数)

这部分代码:

OSCCON = 0b01101000; // 4 MHz

实际上配置微控制器用于其时钟信号的振荡器的频率。但是,重要的是要知道该时钟信号的来源,根据datasheet由配置字1控制。这是在代码的#pragma config FOSC=INTOSC部分设置的。

要获得1 MHz,您需要将该行更改为:

OSCCON = 0b01011000; // 1 MHz

这可以在数据表中的OSCCON寄存器描述中找到。

__delay_ms函数使用_XTAL_FREQ来计算延迟,这就是为什么要让您更改代码的这一行:

#define _XTAL_FREQ 4000000

对此

#define _XTAL_FREQ 1000000
© www.soinside.com 2019 - 2024. All rights reserved.