viernes, 24 de enero de 2014

Eco de señal generada vía Simulink. (Código)

Las lineas de código necesarias para esta practica son las siguientes:

#include"Cpu.h"
#include"Events.h"
#include"mqx_tasks.h"
#include"lwevent.h"
#ifdef __cplusplus
extern"C" {
#endif

#define STRING2INT 1
#define DATARCV 2
#define OFFSET 2048

LDD_TDeviceData *DACptr,*ADCptr;
uint16 MeasuredValues[1];
bool FlgDatorecibido=FALSE,FlgOutData=FALSE;
int dato,SpyRecvData[8];
short posicion;
unsignedchar Dato_ML[16],*ap_dato;

/* LW Event Definitions */
LWEVENT_STRUCT lwevent;
voidTask1_task(uint32_t task_init_data)
{
         ADCptr=AD1_Init(NULL)   //Inicializa ADC
         DACptr=DA1_Init(NULL);  //Inicializa DAC
         Dato_ML[15]='\n';       //Escribe '\n' en ultima posicion para leer primer dato
         AD1_SelectSampleGroup(ADCptr,0U);       // Select sample group 0
         _lwevent_create(&lwevent,LWEVENT_AUTO_CLEAR);     //Crea evento para la tarea 2
         _task_create_at(0, TASK2_TASK, 0, Task2_task_stack, TASK2_TASK_STACK_SIZE);                      
      

while(1) {
         _lwevent_wait_ticks(&lwevent,DATARCV,FALSE,NULL);
       _lwevent_set(&lwevent,STRING2INT); //Habilita el evento para la tarea 2
  }
}
voidTask2_task(uint32_t task_init_data)
{
int counter = 0;
int i,k=0;
short pos_actual;
      
while(1) {
             _lwevent_wait_ticks(&lwevent,STRING2INT,FALSE,NULL);
             pos_actual=(posicion-2)&0xf;   //determina la posicion para el apuntador        
                                            //apuntador posicion-2 debido a 'U' y '\n'
             ap_dato=&Dato_ML[pos_actual];  //Envia apuntador a posicion de las
                                            //unidades del dato a leer
             dato=*ap_dato-'0';             //Convierte ASCII a Decimal '0'= 30
             i=10;                                       
             while(FlgDatorecibido==TRUE){
//Envia apuntador y contador a la ultima posición.
if(pos_actual==0) {ap_dato=&Dato_ML[15];pos_actual=255;}                        else {ap_dato--;pos_actual--;//Decrementa apuntador y contador
                           if(*ap_dato!='\n'){  //Si dato es diferente a '\n'
//Si dato es signo negativo, entonces el dato es negado.
if(*ap_dato=='-')dato=-dato;         
                                  else{//Si no, sumar decenas, centenas y millares.
                                        dato=dato+(*ap_dato-'0')*i;
                                        i=i*10;}//Multiplicador
                           }
                           else {                                   
                                  //Arreglo espia circular de 8 datos
SpyRecvData[k]=dato+OFFSET;          
                                  if(SpyRecvData[k]<0)SpyRecvData[k]=0;
                                  if(SpyRecvData[k]>4094)SpyRecvData[k]=4096;
                                  DA1_SetValue(DACptr,SpyRecvData[k]);
                                  k=(k+1)&0x7; 
                                  FlgDatorecibido=FALSE;
//Habilita al ADC para un sola medicion.
AD1_StartSingleMeasurement(ADCptr);}                        }
         }
}
#include"Cpu.h"
#include"Events.h"
#include"mqx_tasks.h"
#include"lwevent.h"
#ifdef __cplusplus
extern"C" {
#endif

#define OFFSET 2048
#define DATARCV 2

AS1_TError *Err;
LDD_TDeviceData *Serialptr;      
byte CountConvertedData,Error,RecvChrError,SendChrError;
int SpyADC[8];
unsignedchar ConvertedValues[8],*ConvertedValuesptr,SpySendData[32];
externint dato;
externshort posicion;
externunsignedchar Dato_ML[16];
externLDD_TDeviceData *ADCptr;
externuint16 MeasuredValues[1];
externbool FlgDatorecibido,FlgOutData;

/* LW Event Definitions */
LWEVENT_STRUCT lwevent;

/*PROTOTIPO DE FUNCIONES--------------------------------------------------------- */
voidint16toascii(uint16);
voidint2str(int,int);
intnumpow(int,int);
//--------------------------------------------------------------------------------//
voidAS1_OnRxChar(void)    //RECEPCION DE CARACTERES
{
       staticAS1_TComData *ap_buffinput=Dato_ML;
       static int i=0;

             RecvChrError=AS1_RecvChar(ap_buffinput);    //Recibe dato
             if (Dato_ML[i]=='\n'&&Dato_ML[(i-1)&0xf]=='U'){      
                    FlgDatorecibido=TRUE; //Bandera dato recibido
                    posicion=i;
                    _lwevent_set(&lwevent,DATARCV);}                                                        ap_buffinput++;
             i=(i+1)&0xf; 
             if(i==0)ap_buffinput=Dato_ML;
}
voidAS1_OnTxChar(void)
{
       ConvertedValuesptr++;                
       if(*ConvertedValuesptr!=NULL){
             while ((SendChrError=AS1_SendChar(*ConvertedValuesptr))!=ERR_OK);
                                                                     }
}
voidAD1_OnMeasurementComplete(LDD_TUserData *UserDataPtr)
{
       static int i,j,k=0,aux;    
            
       AD1_GetMeasuredValues(ADCptr,MeasuredValues);//Toma valor del ADC
       SpyADC[j]=(MeasuredValues[0]>>4)-OFFSET; 
       int2str(SpyADC[j],6);                   //Llama a función int a string
                   
       //Llena Arreglo Espia de datos enteros convertidos recibidos al ADC
       aux=(CountConvertedData-1)*6;
       for(i=aux;i<=6+aux;i++){                                                 
SpySendData[i]=ConvertedValues[k];//Buffer espia de dato a enviar 
k++;}                                     
       j=(j+1)&0x7;                                                                     
       k=0;
       ConvertedValuesptr=ConvertedValues;                            
       SendChrError=AS1_SendChar(*ConvertedValuesptr);
}

No hay comentarios:

Publicar un comentario