我正在建立一个项目,我想通过IR二极管将模拟信号从mikrocontroller(PIC16F1827)传输到Arduino。 PIC从电位计(10k Ohm)获取模拟信号(0-5 V),并将其与ADC转换为8位数字代码。之后,我想通过IR发送从转换中获取的二进制代码。 我的计划是让输出做一个方波信号,其中“ 1” = HIGH,而“ 0” = LOW。 例如输入= 3.093V,将给出二进制代码“ 10011100”,然后将其转换为方波输出“ HIGH,LOW,LOW,LOW,HIGH,HIGH,HIGH,LOW,LOW”,以便我的IR二极管可以将其作为包装发送。
带有红外发射器的Arduino直接连接到面包板上的Arduino Nano。
PIC代码现在仅生成4000 Hz的PWM信号,但是那是错误的,我没有设法让i在所需的“位信号”中表示它。 PWM信号是正确的,我可以在占空比范围约为(0-92%)如此(0-4.6 V)的示波器上很好地检测PWM信号。
// INCLUDE
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#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)
#define _XTAL_FREQ 4000000 //Klockoscillatorfrekvens Fosc=4 MHz
#define BAUDRATE 9600
//Datasheet PIC16F1827
//PWM on page = DS41391D-page 208
//Prototype ---------------------------------------------------------
void PWM_Initialize();
void ADC_Initialize();
unsigned int ADC_Read(unsigned char channel);
void init();
void main();
//----------------------------
void PWM_Initialize()
{
PR2 = 254;
CCP3CON = 0b00001100; //CCP3 i PWM-mode
CCPTMRS = 0b01001010; //CCP3 use Timer2
T2CON = 0b00100100; //Timer2 On, Pre-scaler = 1 gives PWM = 4 kHz
//PR2 = 0b00011001 ;
//T2CON = 0b00000101 ;
//CCPR1L = 0b00001100 ;
//CCP3CON = 0b00111100 ;
}
//------------------------------
void ADC_Initialize()
{
ADCON0 = 0x01; //ADON
ADCON1 = 0b01000000; //Left written, AD-clock = Fosc / 4, Vref VDD och VSS
ADRESL = 0x00;
ADRESH = 0x00;
}
//------------------------------------------
unsigned int ADC_Read(unsigned char channel)
{
ADCON0 = (ADCON0 & 0b10000011)|(channel<<2); // ADC-channel
__delay_us(5); //Delay 5us. Macro i HITECH C.
ADCON0bits.GO=1; //AD-converter start
while(ADCON0bits.GO); //Wait for AD-convertion to finish
//return ((ADRESH<<8) +ADRESL); //Returns 10 bit Result
return ADRESH; //Return 8 MSB of AD-convertion
}
//----------------------------------------------
void init()
{
OSCCON = 0b01101000; //Intern clock 4 MHz
OSCTUNE = 0x00; // TUN 0;
BORCON = 0x00; // SBOREN disabled;
ANSELA = 0b00000001; //PortA bit 0 (RA0) analog, all other bit digital
ANSELB = 0b00000000; //PORTB alla bits digital
TRISA = 0b00100001; //PORTA bit 0 (RA0) and 5 (RA5) input, rest output
TRISB = 0x00000000; //PORTB all as output
}
//Main-program ----------------------------------------------------------------
void main()
{
int adc_value;
init(); // Init PORTS
ADC_Initialize(); // Initializes ADC Module
PWM_Initialize(); // This sets the PWM frequency of PWM1
do
{
adc_value = ADC_Read(0); //Reading Analog Channel 0 from RA0
PORTB = adc_value; // Indicate bit numbers with 8 LED´s on PORT (RB0-RB7) for tetsing
__delay_ms(50);
CCPR3L = adc_value; // ADC to PWM signal converter
__delay_ms(50);
}while(1); //Infinite Loop
我如何获得这种转换?像这样:
我不知道如何为x us设置方波高,以“在方波输出中代表1”,为y设置方波低,以“在方波输出中代表0”
例如,我如何为Ex编写代码
if ADC_read = "1"
set squarewave HIGH for 600 us;
if ADC_read"0"
set squarewave LOW for 1000 us;
非常感谢您的帮助,谢谢!