#include <18lf2520.h>
#fuses NOMCLR,HS,PROTECT,NOLVP,NOWDT,NODEBUG,CCP2B3,NODEBUG,NOPBADEN
#byte PORTA=0xF80
#byte PORTB=0xF81
#byte PORTC=0xF82
#byte LATC=0xF8B
#byte BAUDCON=0xFB8
#byte SPBRG=0xFAF
#byte HLVDCON=0xFD2
#byte PIR2=0xFA1
#bit HLVDEN=HLVDCON.4
#bit HLVDIF=PIR2.2
#bit PWON=LATC.0
#bit STATE=PORTC.1
#bit Chargement=PORTB.7
#bit MA=PORTB.2
#bit Key=PORTB.5
#bit ModeBT=PORTB.4
#use delay (clock=14745600)//Quartz=14M745600
#use rs232(baud=38400,xmit=PIN_C6,rcv=PIN_C7)
char BTName[16]="CardioSurvey0000";
int DATA[30];
int cosinus[240]={49 50 51 53 55 56 58 59 61 62 63 64 64 64 65 64 64 63 62 61 59 58 56 55 53 52 50 49 47 46 45 44 43 42 42 41 41 40 40 40 39 39 38 36 34 32 29 25 23 21 23 28 38 54 77 106 141 179 213 240 255 254 238 209 172 132 94 62 37 19 8 3 3 6 11 17 22 27 30 33 34 35 36 36 36 37 37 37 38 38 39 39 40 40 41 42 43 43 45 46 47 48 50 51 53 54 56 58 60 62 64 66 68 70 72 74 76 78 79 81 82 84 85 86 86 87 87 87 87 86 86 85 84 83 81 80 78 76 74 72 70 68 66 64 61 59 57 55 53 51 50 48 46 45 44 42 41 40 39 39 38 37 37 36 36 35 35 35 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 34 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 36 36 36 36 36 36 36 36 37 37 37 38 38 39 40 40 41 42 44
};
unsigned int8 j=0,i,count,marche=0;
unsigned int8 LVD;
long compteur=0;
#int_timer2
void CAN()
{
delay_us(3);//attendre 4*Tclk=4/2,048M=1,95µs
if(STATE==1)
{
marche=1;
compteur=0;
DATA[3]=0;
DATA[5]=0;
DATA[4]=cosinus[count];
if(count<239) count++; else count=0;
for(j=0;j<27;j++)
{
//DATA[j+h*27]=spi_read(0);//tt;
putc(DATA[j]);
}
for(j=27;j<30;j++) putc(DATA[j]);
}
/*else
{
if(compteur<9000) compteur++;
else
{
delay_ms(10);
reset_cpu();
}
if (marche==1)
{
delay_ms(10);
reset_cpu();
}
}*/
}
void init_ADS1298(void)
{
//---------------------------------------------------------------------------
spi_write(0x11);//SDATAC pour pouvoir configurer les registres
delay_us(2);// 4/2,048M=1,95µ
spi_write(0x0A);//STOP conversion
delay_us(2);
//---------------------------------------------------------------------------
spi_write(0b01010111);//WREG First opcode
delay_us(2);
spi_write(0b00000010);//Second opcode
delay_us(2);
spi_write(0b11100010);//CONFIG4 adres 17h:Continuous conversion, LOFF Comparator ON
delay_us(2);
spi_write(0b00001010);//WCT1 adres 18h:IN2P to WCTA==>LL
delay_us(2);
spi_write(0b11000001);//WCT2 adres 19h:IN1P to WCTB==>LA et IN1N to WCTC==>RA
delay_us(2);
//---------------------------------------------------------------------------
spi_write(0b01001111);//First opcode :010r rrrr,rrrr is the starting reg
delay_us(2);
spi_write(0b00000001);//Second opcode:000n nnnn,n nnnn the number of reg to write 1
delay_us(2);
spi_write(0b11111111);//LOFF_SENSP adress 0F: DETECTION de déconnection des électrodes P
delay_us(2);
spi_write(0b11111111);//LOFF_SENSP adress 10: DETECTION de déconnection des électrodes N
delay_us(2);
//---------------------------------------------------------------------------
spi_write(0b01000001);//First opcode :010r rrrr,rrrr is the starting reg
delay_us(2);
spi_write(0b00000011);//Second opcode:000n nnnn,n nnnn the number of reg to write 1
delay_us(2);
spi_write(0b00000110);//CONFIG1 adress 01: Hightresol,Daisy-chain,clock out disabled,FS=FCLK/4/128=160000/4/128..
delay_us(2);
spi_write(0b00010000);//CONFIG2 adress 02: Test signal internal, -1m<Amplitude<1m,Pulsed at fCLK/2^21
delay_us(2);
spi_write(0b11001100);//CONFIG3 adress 03: Refernec interne,Vref=2.4V,RLD sens enabled...
delay_us(2);
spi_write(0b01000011);//LOFF adress 04: 90%P, 10%N, Source de courant, DC
delay_us(2);
//---------------------------------------------------------------------------
spi_write(0b01000101);//First opcode :010r rrrr,rrrr is the starting reg
delay_us(2);
spi_write(0x09);//Second opcode:000n nnnn,n nnnn the number of reg to write 1
delay_us(2);
for(j=0;j<8;j++)
{
spi_write(0b00000000);//CHnSET
delay_us(2);
}
spi_write(0b00000011);//RLD_SENSP LL LA
delay_us(2);
spi_write(0b00000001);//RLD_SENSN RA
delay_us(2);
//---------------------------------------------------------------------------
spi_write(0x08);//START command: Debut de conversion
delay_ms(100);//tsattle=18440/2,048M
//---------------------------------------------------------------------------
spi_write(0x10);//Retour en mode RDATAC
delay_us(2);
}
void init_bt()
{
ModeBT=0;
Key=1;
delay_ms(10);
ModeBT=1;
delay_ms(1000);
//printf("AT+ORGL\r\n");
//delay_ms(20);
printf("AT+UART=115200,0,0\r\n");
delay_ms(20);
printf("AT+NAME=%s\r\n",BTName);
delay_ms(20);
printf("AT+IPSCAN=1024,512,1024,512\r\n");//printf("AT+IPSCAN=1024,1,1024,1\r\n");
delay_ms(20);
printf("AT+SNIFF=0,0,0,0\r\n");//printf("AT+SNIFF=40,20,1,8\r\n");
delay_ms(20);
key=0;
ModeBT=0;
delay_ms(20);
ModeBT=1;
}
#int_ext2
void marche_arret()
{
PWON=!PWON;
while(MA==1);
delay_ms(250);//attente demarrage ads1298
switch(PWON)
{
case 1:set_tris_b(0xc7);
set_tris_c(0x9e);
output_bit( PIN_C0, 1);
delay_ms(10);
ModeBT=1;
delay_ms(10);
Key=0;
break;
case 0:delay_ms(10);
reset_cpu();
break;
}
}
void niveau_charge()
{
LVD=0x1D;
setup_low_volt_detect( LVD_TRIGGER_ABOVE | LVD );
delay_us(10);
HLVDIF=0;
while(HLVDIF==0)
{
LVD--;
setup_low_volt_detect( LVD_TRIGGER_ABOVE | LVD );
delay_us(10);
}
switch(LVD)
{
case 0x1C: DATA[0]=(DATA[0]|0x0F)&0xF5; break;//V>4.0V
case 0x1B: DATA[0]=(DATA[0]|0x0F)&0xF4; break;//V>3.8V
case 0x1A: DATA[0]=(DATA[0]|0x0F)&0xF3; break;//V>3.6V
case 0x19: DATA[0]=(DATA[0]|0x0F)&0xF2; break;//V>3.5V
case 0x18: DATA[0]=(DATA[0]|0x0F)&0xF1; break;//>V3.3V
case 0x17: DATA[0]=(DATA[0]|0x0F)&0xF0; break;//3.0V<V<3.3V
}
}
void main(void)
{
PORTB=0x00;
PORTC=0x00;
DATA[27]='S';DATA[28]='Y';DATA[29]='N';
marche=0;
set_tris_b(0xc7);
set_tris_c(0x9e);
//BAUDCON=0x00;
for(i=0;i<27;i++) DATA[i]=0;
if(read_eeprom(0)==0xFF)
{
#use rs232(baud=38400,xmit=PIN_C6,rcv=PIN_C7)
//SPBRG=5;//38400=(14745600/64/(5+1))
init_bt();
write_eeprom (0,0x00);
}
//#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
SPBRG=1;//115200=(14745600/64/(1+1))
//---------------------------------------------------------------------------
//delay_ms(64);//Delais de démarrage du ADS1298=2^16/2,048Mhz=32mS
//---------------------------------------------------------------------------
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_4,255,12);//f=(11059200/4/4/(255+1)/12)=300Hz Simulation
PWON=0;
ModeBT=0;
Key=0;
ext_int_edge(L_TO_H);
enable_interrupts(INT_EXT2);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);
//delay_us(10);
//if(PWON==1) goto START;