I´m building a project where i want to transfer an analog signal from a mikrocontroller (PIC16F1827) to an Arduino via IR-diodes. The PIC takes a analog signal (0-5 V) from a potentiometer (10k Ohm) and converts it with ADC to 8-bit digital code. After that i want to send the binary code that i get from the conversion over IR. My plan is to have the output do a squarewave signal where the a "1" = HIGH and a "0" = LOW. ex. input = 3.093V which gives the binary code "10011100" this would then be converted to a squarwave output "HIGH,LOW,LOW,HIGH,HIGH,HIGH,LOW,LOW" so that my IR diode can send it as a package.
The Arduino with an IR transmitter is connected directly to an Arduino Nano on my bread board.
The PIC code only generates a 4000 Hz PWM signal right now but thats wrong, i have not managed to get i to represent it in the wanted "bit signal". The PWM signal is correct and I can se the PWM signal good on my oscilloscope where the duty cycle span is around (0-92%) so around (0-4.6 V).
// 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
How do i get this conversion? Like this:
I have no knowledge i how to set a squarewave HIGH for x us "to represent a 1 in the squarewave output" and set the squarewave LOW for y us "to represent a 0 in the squarewave output"
so for exempel how do i write a code that for Ex
if ADC_read = "1"
set squarewave HIGH for 600 us;
if ADC_read"0"
set squarewave LOW for 1000 us;
Would appreciate any help, Thanks!
User contributions licensed under CC BY-SA 3.0