Sie sind auf Seite 1von 3

#include <p18f4321.

h>
#include <stdio.h>
#include <math.h>
#include <usart.h>

#pragma config OSC = INTIO2


#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config BOR =OFF

char COUNTER = 0;
char ARRAY[60];

#define Chip_Select PORTAbits.RC0

//Routine for setting A/D


void Init_ADC(void)
{
ADCON0=0x09; // select channel AN0, and turn on the
ADC subsystem
ADCON1=0x0F; // select pins AN0 through AN3 as analog
signal, VDD-VSS as
ADCON2=0xA9; // right justify the result. Set the
bit conversion time (TAD) and
// acquisition time
}
//Routine for setting inputs/outputs
void DO_INIT()
{
Init_ADC();
TRISA = 0x00; //Set Ports A as inputs

OSCCON=0x60; // Program oscillator to be at 4Mhz


TRISC=0x00; // Setup port C with output
SSPSTAT=0x40; // SMP:
// Input data sampled at middle of data
output
// CKE:
// Transmit occurs on transition from
active
SSPCON1=0x20; // SSPEN:
// Enables serial port and configures
SCK, ?
T0CON=0x08; // Set Timer 0 in Timer mode
TMR0H=0xFE; // Program Timer High byte
TMR0L=0x0B; // Program Timer Low byte
INTCONbits.TMR0IE=1; // Enable Timer 0 interrupt
INTCONbits.TMR0IF=0; // Clear Timer 0 Interrupt Flag
INTCONbits.GIE=1; // Enable Global Interrupt
T0CONbits.TMR0ON=1; // Turn on Timer 0

void SPI_out(unsigned char SPI_data)


{
int i;
char First_byte, Second_byte;
First_byte = (SPI_data & 0xf0) >> 4; // take the upper nibble of data and >>
4
//times
First_byte = 0x30 | First_byte; // set the upper nibble with 0x30
Second_byte = (SPI_data & 0x0f) <<4; // take the lower nibble of data and <<
4 times

Chip_Select = 0; // set the chip select of the D/A chip


to be low

SSPBUF = First_byte; // output the first byte to the SPI bus


while (SSPSTATbits.BF == 0); // wait for status done
for (i=0;i<1;i++); // small delay

SSPBUF = Second_byte; // output the second byte to the SPI


bus
while (SSPSTATbits.BF == 0); // wait for status done
for (i=0;i<1;i++); // small delay

Chip_Select = 1; // raise chip select high


}

//Routine for Array


void DO_ARRAY_INIT()
{

for(int I = 0; I < 60; I++)


{
if(I < 30)
{
ARRAY[I] = 0;
}
else
{
ARRAY[I] = 255;
}
}
}
void interrupt high_priority T0ISR()
{
INTCONbits.TMR0IF=0; // Clear Timer 0 Interrupt Flag
TMR0H=0xFE; // Program Timer High byte
TMR0L=0x31; 4C // Program Timer Low byte
SPI_out(ARRAY[COUNTER]);
COUNTER++;
if(COUNTER == 60)
{
COUNTER = 0;
}
}

void main()
{
DO_INIT();
DO_ARRAY_INIT(); //Call function ARRAY
while(1)
{
}
}

Das könnte Ihnen auch gefallen