【发布时间】:2018-09-06 15:03:13
【问题描述】:
对于下面的问题 1,我真的被困在 a 和 b 部分。我真的很困惑如何使用 > 函数来更改乘法和除法函数以更改引脚/LED。 任何帮助将非常感激。谢谢!
多个 LED 以及使用和输入按钮
- 修改C程序:
一个。不要使用乘法和除法函数来更改引脚/LED,而是使用 > 函数。参考:Deitel 和 Deitel “C,如何编程和https://en.wikipedia.org/wiki/Operators_in_C_and_C
b.将程序中的时钟频率更改为 1 MHz,并使每个 LED 的开/关时间为 0.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(任何版本)
用于研究的 Internet 浏览器搜索引擎(Google、Bing 等) 上传_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; }
}
}
}
【问题讨论】:
-
请先格式化您的代码。空行和缩进对编译器可能无关紧要,但对于试图阅读您的代码的人来说却很重要。
-
这里是乘法代码:i1=i1*2;这是除法代码:i1=i1/2;完全不知道从哪里开始更改它以使用 > 功能来更改引脚/LED。
-
请edit您的问题添加信息,而不是将其隐藏在 cmets 中。
-
我相信我现在掌握了这一点。感谢您耐心等待我学习这一点。所以 i1=i1*2 将是 i1=i1>1(i1 的值将在二进制代码中向右移动 1,因此将其除以 2。)
标签: c microprocessors