Sie sind auf Seite 1von 8

/

*************************************************************************
/
/* Files to Include
*/
/
*************************************************************************
/
#include <p30F4013.h>
/* Device header file */
#include "system.h"
/
*************************************************************************
*/
#define __main_c__
#include "user.h"
/* Global Variable Declaration
*/
/
*************************************************************************
*/
unsigned int cVariable,temp;
/********************************************************/
unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT,
SET_FREQ;
unsigned short TBL_temp;
unsigned int DUTY_CYCLE;
/************* glolbal varialble declaration***********/
/*---------------------*/
volatile char tmr1_intrpt_flag;
volatile char
uart1_rx_flag,adc_int_flag,adc_int_intex;//
volatile int
adc_buff0,adc_buff1,div_val;
volatile unsigned int
tmr1_onesec_count,tmr4_val_hold;
volatile unsigned char uart_rx_arry[6],arr_1_intex;/*-----UART1 RX
data----*/
/*---------------------*/
extern int
kp_const_q6,ki_const_q3,delta_const_q5;
/*---------------------*/
/******************************************************/
char
curr_state;
/*----------------------*/
int
pi_cntrl_out_var,an_ip_error;
int oc1_duty_val,oc2_duty_val,oc3_duty_val,oc4_duty_val,oc3_duty;
unsigned char
uart1_rx_data_1,uart1_rx_data_2,uart1_rx_data_3,uart1_rx_data_4,rx_data_w
rd;
//
int pwm_onoff_cntr;
//
int
uart_rx_parm_99,uart_rx_parm_88,rx_data_h; //
char
uart_tx_ok,uart_tx_packet;
//
int trig_CN0_val,trig_CN1_val;
//
int
an_ch0_val,an_ch1_val;
/*----------------------*/

/
*************************************************************************
*****/
/* Main Program
*/
/
*************************************************************************
*****/
int main(void)
{
/* Startup Delay */
DelayMs(500);
DelayMs(500);
DelayMs(500);
DelayMs(500);
/*%%%%%%%%%%%%%%%%%%%%%%*/
tmr1_onesec_count
=
0;
CORCONbits.SATA
=
0;
enabled for AccuA and AccuB
CORCONbits.SATB
=
0;
for DSP multiply operation*/
CORCONbits.IF
=
1;
0=fract)
CORCONbits.ACCSAT
=
0;
allowed
CORCONbits.US
=
1;
are unsigned(1=unsig,0=sign)
CORCONbits.SATDW
=
0;
saturation in data space

//32bit saturation

tmr1_intrpt_flag
curr_state
state
an_ch0_val
an_ch1_val

=
=

0;
0;

// clear tmr1 isr flag


// set first value of

=
=

0;
0;

kp_const_q6
ki_const_q3
delta_const_q5
DECIMEL)

=
=
=

0x3D;
8;
3;

InitApp
();
config_gpio ();
ADC_Init
();
//_LATB5=1;
DelayMs(100);
//_LATB5=0;
LCD_INIT() ;
DelayMs(250);
LCD_INIT() ;
Pwm_oc1_oc2_Init ();
initialisation
Pwm_oc3_oc4_Init ();
Uart1_init ();
// SPI_Init ();
uart_tx_packet
=
uart_tx_ok
=

/*intiger mode enabled


// intiger mode (1=int,
// 40bit accumulator
// DSP multiplications
//disable 16bit

// 0.95*2^6 = 61(0x3D)
// 1*2^3 = 8
// 0.1*2^5 = 3(in

// IO configure
// ADC confi

// PWM OC1-OC2
// PWM OC3-OC4 Initialisation
//UART 1 initialisation
0;
0;

pi_cntrl_out_var
curr_state
uart1_rx_data_1
uart1_rx_data_2
uart1_rx_data_3
pwm_onoff_cntr
OC1RS = 0;
OC2RS = 0;
OC3RS = 0;
OC4RS = 0;

=
=
=
=
=
=

0;//
0;
0;
0;
0;
0;

adc_int_intex

0xF;

/*%%%%%% Sine PWM configuration %%%%%%%%%*/


TBL_POINTER_NEW
=
0;
TBL_POINTER_OLD
=
0;
TBL_POINTER_SHIFT
=
0;
SET_FREQ
=
410; // Equ to 50HZ
DUTY_CYCLE
=
0;
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
//LCD_PUTCH(72); //H
//LCD_PUTCH(101); //e
//LCD_PUTCH(108); //l
//LCD_PUTCH(108); //l
//LCD_PUTCH(111); //o
//DelayMs(500);
//DelayMs(500);
//LCD_CLEAR ();
// LCD_PUTS("hi");
//putdec(0xFFF);
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% LCD display %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
unsigned int K;
K=8888;
LCD_PUTUN(4,K);
LCD_GOTO (1,6);
LCD_PUTS("vol");
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
tmr4_Init
();
// Low priority task
tmr1_Init
();
// Timer 1 control state machine, start configuration
while(1){
//state machine//
if(tmr1_intrpt_flag==1)
{
//_LATB5
^=
1;
switch(curr_state)
{
case 0:
LCD_GOTO(1,1);
//LCD_PUTUN(4,adc_buff1);
div_val=adc_buff1;
K = div_ab(0x7271,div_val); // not corrected.
can not use
LCD_PUTUN(4,div_val);
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

/*%%% calling asm function %%%%*/


/* %% blocked entire code.
cVariable = 0x50;
temp=asmFunction();
//
if(temp==0x61){
// _LATB5=1;
}
//calling ASM function for SUM
cVariable = asmsum(337,100);
//if result is correct LED is ON
if(cVariable==0x83AE)
{
_LATC15 =1; //LED2
} ;*/
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
trig_CN0_val =
trig_CN1_val =

PORTCbits.RC14;
PORTCbits.RC13;

if(tmr1_onesec_count==1)
{
//_LATB5 ^= 1; // LED1
uart_tx_packet = uart_tx_packet+1;
if(uart_tx_packet>4)
{
uart_tx_packet=1;
};
uart_tx_ok = 1;

// =1

for uart transmit


tmr1_onesec_count =0;
};
if (uart1_rx_flag==1)
{
uart1_rx_flag = 0;
// reset UART recive flag
arr_1_intex
=
UART1_RX_1;
// first byte => command to identify input
uart1_rx_data_1 =
uart_rx_arry[arr_1_intex];
arr_1_intex
=
UART1_RX_2;
// second byte => data higher byte
uart1_rx_data_2 =
uart_rx_arry[arr_1_intex];
arr_1_intex
=
UART1_RX_3; // third
byte => data lower byte
uart1_rx_data_3 =
uart_rx_arry[arr_1_intex];
arr_1_intex
=
UART1_RX_4;// fourth
byte => data TBD
uart1_rx_data_4 =
uart_rx_arry[arr_1_intex];
}
// return uart_rx_flag==1
/*=== decode command and make word of data ====*/

// combine higher byte and lower byte for 16bit data


// if command is 0x99 => parameter is "uart_rx_parm_99"
// identify uart command parameter "uart1_cmd_parm"
if (uart1_rx_data_1 == 0x99)
{
rx_data_h
= shift_int(uart1_rx_data_2,8);
uart_rx_parm_99 =
sign_add(rx_data_h,uart1_rx_data_3);
}
else if(uart1_rx_data_1 == 0x88)
{
rx_data_h = shift_int(uart1_rx_data_2,8);
uart_rx_parm_88 =
sign_add(rx_data_h,uart1_rx_data_3);
}
else if(uart1_rx_data_1 == 0x77)
{
pwm_onoff_cntr
= uart1_rx_data_2;
}
/*========== REFERENCE INPUT ================*/
/*---- Read ADC channel "0"
//an_ch0_val

--------*/

adc_start_sample(0);
//an_ch0_val = uart_rx_parm_88;
/*---- read reference from UART1 RX data--*/
an_ch0_val = sign_mpy(adc_buff0,5,-16);
//an_ch1_val = uart_rx_parm_99;
an_ch1_val = sign_mpy(adc_buff1,5,-16);
/*============= Feedback ====================*/
/*---- Read ADC channel "1"
//an_ch1_val

-----*/

adc_start_sample(1);

an_ip_error

= an_ch0_val-an_ch1_val;

pi_cntrl_out_var =
pi_1_contrller(an_ch0_val,an_ch1_val); /*----- call PI controller
------*/
if(pi_cntrl_out_var<=0)
{
pi_cntrl_out_var=0;
}

curr_state
tmr1_intrpt_flag
break;

=
=

1;// set next state


0;

/*----------------------------------*/
case 1:
// oc1_duty_val = sign_add(pi_cntrl_out_var,10);
//update pwm duty cycle
//OC1RS
= oc1_duty_val;
//OC1RS
= 0x7271;
// 1%->0x24A,5%->0xB72,50%->0x7271,90%->0xCDFE
/*Vin = 25.18V
Vout = 46.16V
OC1RS = 650
------------Vin = 25.18V
Vout = 49.53V
OC1RS = 700
*/
//oc2_duty_val = sign_add(pi_cntrl_out_var,10);
// OC2RS
= oc2_duty_val;

// oc3_duty_val = sign_add(pi_cntrl_out_var,-600);
if(oc3_duty_val<=0)
{
oc3_duty_val=0;
}
//OC3RS
= oc3_duty_val;
OC3RS = 0x7271 ;// for testing OC3
OC4RS = 0x7271 ;// for testing OC4

// oc4_duty_val = sign_add(pi_cntrl_out_var,10);
// OC4RS
= oc4_duty_val;
if (pwm_onoff_cntr==1)
{
OC1RS = 0;
OC2RS = 0;
OC3RS = 0;
OC4RS = 0;
}
/*=====

If Transmit command == 99 */
if(uart_tx_ok==1&&uart_tx_packet==1)
{

write_char_toUART1(1);
write_word_toUART1(an_ch0_val);
uart_tx_ok = 0;//reset flag
}
curr_state
tmr1_intrpt_flag

=
=

2; // set next state


0;

break;
case 2:
/*=====

Transmit data to UART 1====*/

/*=====

If Transmit command == 88 */
if(uart_tx_ok==1&&uart_tx_packet==2)
{
write_char_toUART1(2);
write_word_toUART1(an_ch1_val);
uart_tx_ok =
0;//reset flag
}
curr_state
tmr1_intrpt_flag
break;

=
=

3;
0;

// set next state

case 3:
adc_int_intex = 0; // channel "0"
adc_start_sample(0);
/*=====

Transmit data to UART 1====*/


if(uart_tx_ok==1&&uart_tx_packet==3)
{
write_char_toUART1(3);
write_word_toUART1(pi_cntrl_out_var);
uart_tx_ok =
0;//reset flag
}
curr_state
tmr1_intrpt_flag
break;

=
=

4; // set next state


0;

case 4:
adc_int_intex = 1;
adc_start_sample(9);// channel "9"

if(uart_tx_ok==1&&uart_tx_packet==4)
{
write_char_toUART1(4);
write_word_toUART1(an_ip_error);
uart_tx_ok =
0;//reset flag/stop Transmission
}
curr_state
tmr1_intrpt_flag
break;
default:
break;
}
}
}

=
=

0; // set next state


0;

// return switch func


// return "IF" loop
//return while loop
}
// return main
/*---------------------------------------*/

Das könnte Ihnen auch gefallen