/Designs/MRAKOMER4/SW/irmrak4.c
24,7 → 24,7
int8 heat; // Status variables
int8 open;
 
inline void toggle_dome(void)
inline void toggle_dome(void) // Wire exercise
{
if (open>0)
{output_toggle(DOME);}
32,7 → 32,7
{output_low(DOME);}
}
 
void delay(int16 cycles)
void delay(int16 cycles) // Wire exercise with delay
{
int16 i;
 
39,10 → 39,18
for(i=0; i<cycles; i++) {toggle_dome(); delay_us(100);}
}
 
void welcome(void) // Welcome message
{
printf("\n\r* Mrakomer %s (C) 2007 KAKL *\n\r",VER); // Welcome message
printf("* %s *\n\r",REV);
printf("<#sequence> <ambient [1/100 C]> <sky [1/100 C]> ");
printf("<heating [s]> <dome [s]>\n\r\n\r");
}
 
#include "smb.c"
 
#include "smb.c" // System Management Bus driver
 
 
// Read sensor RAM
// Returns temperature in °K
int16 ReadTemp(int8 addr, int8 select)
53,17 → 61,17
 
addr<<=1;
 
SMB_STOP_bit(); //If slave send NACK stop comunication
SMB_START_bit(); //Start condition
SMB_STOP_bit(); //If slave send NACK stop comunication
SMB_START_bit(); //Start condition
SMB_TX_byte(addr);
SMB_TX_byte(RAM_Access|select);
SMB_START_bit(); //Repeated Start condition
SMB_START_bit(); //Repeated Start condition
SMB_TX_byte(addr);
arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK
arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK
arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK
arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK
temp=MAKE16(arr[1],arr[2]);
crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK
SMB_STOP_bit(); //Stop condition
crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK
SMB_STOP_bit(); //Stop condition
 
arr[5]=addr;
arr[4]=RAM_Access|select;
74,6 → 82,7
return temp;
}
 
/*-----------------------------------------------------------------------*/
void main()
{
unsigned int16 seq, temp, tempa;
80,6 → 89,7
signed int16 ta, to;
int8 safety_counter;
 
output_low(DOME); // Close Dome
output_low(HEATING); // Heating off
setup_wdt(WDT_2304MS); // Setup Watch Dog
setup_adc_ports(NO_ANALOGS);
94,30 → 104,25
 
delay_ms(1000);
restart_wdt();
printf("\n\r* Mrakomer %s (C) 2007 KAKL *\n\r",VER); // Welcome message
printf("* %s *\n\r",REV);
printf("<#sequence> <ambient [1/100 C]> <sky [1/100 C]> ");
printf("<heating [s]> <dome [s]>\n\r\n\r");
 
welcome();
tempa=ReadTemp(SA, RAM_Tamb); // Dummy read
temp=ReadTemp(SA, RAM_Tobj1);
 
seq=0;
seq=0; // Variables initiation
heat=0;
open=0;
 
// enable_interrupts(GLOBAL);
// enable_interrupts(INT_RDA);
 
//---WDT
restart_wdt();
 
while(TRUE)
while(TRUE) // Main Loop
{
while(kbhit()) getc(); // Flush USART buffer
CREN=0; CREN=1; // Reinitialise USART
 
safety_counter=0;
 
safety_counter=SAFETY_COUNT; // Heating and Dome Count Down
do
{
if (safety_counter<SAFETY_COUNT) safety_counter++;
143,10 → 148,10
restart_wdt();
}
} while (!kbhit());
 
//---WDT
restart_wdt();
{
{ // Retrieve command
char ch;
 
ch=getc();
165,9 → 170,18
open=MAXOPEN; // Open the dome
break;
 
case 'x':
open=MAXOPEN; // Open the dome
heat=MAXHEAT; // Need heating
break;
 
case 'l':
open=0; // Lock the dome
break;
 
case 'i':
welcome(); // Information about version, etc...
break;
}
}
 
180,8 → 194,8
to=temp*2-27315;
 
{ // printf
char output[10]; // Output buffer
int8 j; // Counter
char output[8]; // Output buffer
int8 j; // String pointer
 
delay(SEND_DELAY);
sprintf(output,"#%Lu ", seq);
/Designs/MRAKOMER4/SW/irmrak4.h
15,6 → 15,5
#FUSES NOIESO //Internal External Switch Over mode disabled
 
#use delay(clock=8000000)
#use rs232(baud=2400,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8)//,FORCE_SW)
//#use i2c(MASTER,SLOW,sda=PIN_B1,scl=PIN_B0,SMBUS)
#use rs232(baud=2400,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8)
 
/Designs/MRAKOMER4/SW/smb.c
6,19 → 6,19
#define RAM_Tamb 0x06 // Ta address in the RAM
 
 
//*High and Low level of clock
// High and Low level of clock
#define HIGHLEV 40 // max. 50us
#define LOWLEV 100 // max. 30ms
#define TBUF 20
 
//SMBus control signals
// SMBus control signals
#define SCL PIN_B0
#define SDA PIN_B1
 
#define mSDA_HIGH() output_float(SDA); // SDA float
#define mSDA_LOW() output_low(SDA); // SDA low
#define mSCL_HIGH() output_float(SCL); //output_high(SCL); // SCL high
#define mSCL_LOW() output_low(SCL); // SCL low
#define mSDA_HIGH() output_float(SDA); // SDA float
#define mSDA_LOW() output_low(SDA); // SDA low
#define mSCL_HIGH() output_float(SCL); // SCL float
#define mSCL_LOW() output_low(SCL); // SCL low
 
#define ACK 0
#define NACK 1
37,18 → 37,19
{
// disable_interrupts(GLOBAL);
mSDA_HIGH(); // Set SDA line
delay_us( TBUF ); // Wait a few microseconds
delay_us( TBUF ); // Wait a few microseconds
mSCL_HIGH(); // Set SCL line
delay_us( TBUF ); // Generate bus free time between Stop
// and Start condition (Tbuf=4.7us min)
mSDA_LOW(); // Clear SDA line
delay_us( TBUF ); // Hold time after (Repeated) Start
// Condition. After this period, the first clock is generated.
//(Thd:sta=4.0us min)
delay_us( TBUF ); // Generate bus free time between Stop
// and Start condition (Tbuf=4.7us min)
mSDA_LOW(); // Clear SDA line
delay_us( TBUF ); // Hold time after (Repeated) Start Condition.
// After this period, the first clock is generated.
// (Thd:sta=4.0us min)
mSCL_LOW(); // Clear SCL line
// enable_interrupts(GLOBAL);
delay_us( TBUF ); // Wait a few microseconds
delay_us( TBUF ); // Wait a few microseconds
}
 
//*********************************************************************************************
// STOP CONDITION ON SMBus
//*********************************************************************************************
63,12 → 64,12
{
// disable_interrupts(GLOBAL);
mSDA_HIGH();
mSCL_LOW(); // Clear SCL line
delay_us( TBUF ); // Wait a few microseconds
mSCL_LOW(); // Clear SCL line
delay_us( TBUF ); // Wait a few microseconds
mSDA_LOW(); // Clear SDA line
delay_us( TBUF ); // Wait a few microseconds
delay_us( TBUF ); // Wait a few microseconds
mSCL_HIGH(); // Set SCL line
delay_us( TBUF ); // Stop condition setup time(Tsu:sto=4.0us min)
delay_us( TBUF ); // Stop condition setup time(Tsu:sto=4.0us min)
mSDA_HIGH(); // Set SDA line
// enable_interrupts(GLOBAL);
}
99,10 → 100,10
unsigned char Ack_bit;
 
// disable_interrupts(GLOBAL);
mSDA_HIGH(); //_SDA_IO=1; // SDA-input
mSDA_HIGH(); //_SDA_IO=1; // SDA-input
mSCL_HIGH(); // Set SCL line
delay_us( HIGHLEV ); // High Level of Clock Pulse
if(input(SDA)) Ack_bit=1; // \ Read acknowledgment bit, save it in Ack_bit
if(input(SDA)) Ack_bit=1; // \ Read acknowledgment bit, save it in Ack_bit
else Ack_bit=0; // /
mSCL_LOW(); // Clear SCL line
 
134,13 → 135,13
for(Bit_counter=8; Bit_counter; Bit_counter--)
{
if(Tx_buffer&0x80) bit_out=1; // If the current bit of Tx_buffer is 1 set bit_out
else bit_out=0; // else clear bit_out
else bit_out=0; // else clear bit_out
 
SMB_send_bit(bit_out); // Send the current bit on SDA
Tx_buffer<<=1; // Get next bit for checking
SMB_send_bit(bit_out); // Send the current bit on SDA
Tx_buffer<<=1; // Get next bit for checking
}
 
Ack_bit=SMB_Receive_bit(); // Get acknowledgment bit
Ack_bit=SMB_Receive_bit(); // Get acknowledgment bit
 
return Ack_bit;
}
161,7 → 162,7
 
for(Bit_Counter=8; Bit_Counter; Bit_Counter--)
{
if(SMB_Receive_bit()) // Get a bit from the SDA line
if(SMB_Receive_bit()) // Get a bit from the SDA line
{
RX_buffer <<= 1; // If the bit is HIGH save 1 in RX_buffer
RX_buffer |=0b00000001;
173,7 → 174,7
}
}
 
SMB_send_bit(ack_nack); // Sends acknowledgment bit
SMB_send_bit(ack_nack); // Sends acknowledgment bit
 
return RX_buffer;
}
196,11 → 197,11
crc[2]=0;
crc[1]=0x01;
crc[0]=0x07;
BitPosition=47; /* Set maximum bit position at 47 */
BitPosition=47; /* Set maximum bit position at 47 */
shift=0;
 
//Find first 1 in the transmited message
i=5; /* Set highest index */
i=5; /* Set highest index */
j=0;
while((pec[i]&(0x80>>j))==0 && i>0)
{