Documente Academic
Documente Profesional
Documente Cultură
h>
#include <math.h>
#include <ctl_api.h>
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
//ISR Initialisation
// Globals declaration
int result=0,timer1_counter = 0;
int voltage=0;
float duty;
bool bADCDataReady = false ;
uint32_t adcAcc = 0 ;
uint32_t adcCount = 0 ;
char String[20],timerready = false;
int main(void)
{
initClocks();
pwm_init();
UART0_init();
ADC_init();
timer0_init();
timer1_init();
while(1)
{
if( bADCDataReady == true )
{
bADCDataReady = false ;
if(timerready == true)
{
sprintf(String,"adc value= %d\n\r\n",voltage);
Send_String(String);
timerready = false;
}
}
}
}
void initClocks(void)
{
PLLCON = 0x01; //Enable PLL
PLLCFG = 0x22; //Multiplier and divider setup
PLLFEED = 0xAA; //Feed sequence
PLLFEED = 0x55;
void timer0_init(void)
{
volatile unsigned int x;
T0TCR = 0; // Reset timer 0
T0PR = 0 ; // Set the timer 0 prescale counter
T0MR0 = 6000; // Set time 0 match register to generate an interrupt
every 100 us(for 40khz) (CCLK is 60mhz)
T0MCR = 3; // Generate interrupt and reset counter on match
T0TCR = 1; // Start timer 0
void timer1_init(void)
{
T1TCR = 0; // Reset timer 1
T1PR = 0 ; // Set the timer 1 prescale counter
T1MR0 = 30000; // Set time 1 match register to generate an interrupt
every 500 us(for 2khz) (CCLK is 60mhz)
T1MCR = 3; // Generate interrupt and reset counter on match
T1TCR = 1; // Start timer 1
void ADC_init(void)
{
PINSEL0 = (PINSEL0 | 0x0300) ; //select AD0.6 for P0.4
ctl_set_isr(18, 5, CTL_ISR_TRIGGER_FIXED, AD0ISR, 0);
ctl_unmask_isr(18);
ctl_global_interrupts_enable();
unsigned long AD0CR_setup = (CLKDIV<<8) | BURST_MODE_OFF | PowerUP;
AD0CR = AD0CR_setup | SEL_AD06;
AD0CR |= START_NOW; //Start new Conversion
}
void pwm_init()
{
PINSEL1 = (PINSEL1 & ~(1 << 11)) | (1 << 10); //(PINSEL0 & ~(1 << 14)) | (1 <<
15); //0x8000;// // Select PWM3 output for P0.1
PWMPCR = 0x0; //Select Single Edge PWM - by
default its single Edged so this line can be removed
PWMPR = 0; // 1 micro-second resolution
PWMMR0 = 1500; //25us PWM pulse (ie., 40khz
frequency)
PWMMR5 = 0; // 0.5ms - pulse duration i.e width
(Brigtness level)
PWMMCR = (1<<1); // Reset PWMTC on PWMMR0 match
PWMLER = (1<<0)|(1<<5); // update MR0 and MR5
PWMPCR = (1<<13); // enable PWM output
PWMTCR = (1<<1) ; //Reset PWM TC & PR
PWMTCR = (1<<0) | (1<<3); // enable counters and PWM Mode
}
//ADC Conversion
void adc_start()
{
AD0CR |= START_NOW; //Start new Conversion
AD0INTEN = ADINTEN6;
}
//---------------------- Timer 0 ISR every 25 us -----------------//
void timer1ISR(void)
{
++timer1_counter;
if(timer1_counter>2000)
{
timerready = true;
timer1_counter = 0;
}
VICVectAddr = 0x0;
T1IR = 0xFF; // Clear the timer 1
interrupt
}
void UART0_init(void)
{
PINSEL0 = (PINSEL0 & ~(1 << 1)) | (1 << 0);
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop
bit | DLAB set to 1 */
U0DLL = 110;//135;
U0DLM = 1;
U0FDR = 0xF1; /* MULVAL=15(bits - 7:4) ,
DIVADDVAL=0(bits - 3:0)*/
U0LCR &= 0x0F; // Set DLAB=0 to lock MULVAL
and DIVADDVAL
}