unsigned char tc600_read(unsigned short param, char *result) { char xdata str[32]; unsigned char xdata i, j, cs, len; sprintf(str, "%03Bd00%03d02=?", TC600_ADDRESS, param); for (i = cs = 0; i < 12; i++) cs += str[i]; sprintf(str + 12, "%03Bd\r", cs); for (i=0 ; i<strlen(str); i++) putchar1(str[i]); flush(); i = gets_wait(str, sizeof(str), 20); if (i > 2) { len = (str[8] - '0') * 10 + (str[9] - '0'); for (j = 0; j < len; j++) result[j] = str[10 + j]; result[j] = 0; return len; } return 0; }
void FastWrite(BYTE addr, BYTE cmd, char data1, char data2) // "Быстрая" отправка сообщения без подтвеждения приема // <адресат>, <команда>, <данные1>, <данные2> // Отправитель формируется автоматически { BYTE ADDR; ADDR = FORMADDR(addr, MY_ADDR); U485CTL = U485WRITE; putchar1(ADDR); putchar1(cmd); putchar1(data1); putchar1(data2); U485CTL = U485READ; }
void printstr1(char* st) { int i; for(i = 0; st[i]!= 0; i++) { putchar1(st[i]); } }
unsigned char tc600_write(unsigned short param, unsigned char len, unsigned long value) { char xdata str[32]; unsigned char xdata i, cs; if (len == 6) sprintf(str, "%03Bd10%03d06%06ld", TC600_ADDRESS, param, value); else if (len == 3) sprintf(str, "%03Bd10%03d03%03ld", TC600_ADDRESS, param, value); for (i = cs = 0; i < 16; i++) cs += str[i]; sprintf(str + 16, "%03Bd\r", cs); for (i=0 ; i<strlen(str); i++) putchar1(str[i]); flush(); i = gets_wait(str, sizeof(str), 50); return i; }
void main(void) { // Declare your local variables here // Crystal Oscillator division factor: 1 #pragma optsize- CLKPR=0x80; CLKPR=0x00; #ifdef _OPTIMIZE_SIZE_ #pragma optsize+ #endif // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=P State2=P State1=P State0=P PORTA=0x0F; DDRA=0x00; // Port B initialization // Func7=Out Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=0 State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0x00; //SerIRQ if input // Port C initialization // Func7=In Func6=In Func5=Out Func4=Out Func3=Out Func2=Out Func1=In Func0=In // State7=T State6=T State5=1 State4=1 State3=1 State2=1 State1=T State0=T PORTC=0x3C; DDRC=0x3C; // Port D initialization // Func7=Out Func6=In Func5=Out Func4=In Func3=Out Func2=In Func1=In Func0=In // State7=1 State6=T State5=0 State4=T State3=0 State2=T State1=T State0=T PORTD=0x80; DDRD=0xA8; // Port E initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=T PORTE=0x00; DDRE=0x02; // Port F initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTF=0x00; DDRF=0x00; // Port G initialization // Func4=In Func3=In Func2=In Func1=In Func0=In // State4=T State3=T State2=T State1=T State0=T PORTG=0x00; DDRG=0x00; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 1000,000 kHz // Mode: CTC top=OCR0 // OC0 output: Disconnected TCCR0A=0x0A; TCNT0=0x00; // OCR0A=((OSCILLATOR_FREQUENCY/8)*0.0001)-1; //((OSCILLATOR_FREQUENCY/8)*0.0001)-1 => 100uS OCR0A=((OSCILLATOR_FREQUENCY/8)*0.001)-1; //((OSCILLATOR_FREQUENCY/8)*0.0001)-1 => 100uS // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer 1 Stopped // Mode: Normal top=FFFFh // OC1A output: Discon. // OC1B output: Discon. // OC1C output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge // Timer 1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off // Compare C Match Interrupt: Off TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; OCR1CH=0x00; OCR1CL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2 output: Disconnected ASSR=0x00; TCCR2A=0x00; TCNT2=0x00; OCR2A=0x00; // Timer/Counter 3 initialization // Clock source: System Clock // Clock value: Timer 3 Stopped // Mode: Normal top=FFFFh // Noise Canceler: Off // Input Capture on Falling Edge // OC3A output: Discon. // OC3B output: Discon. // OC3C output: Discon. // Timer 3 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off // Compare C Match Interrupt: Off TCCR3A=0x00; TCCR3B=0x00; TCNT3H=0x00; TCNT3L=0x00; ICR3H=0x00; ICR3L=0x00; OCR3AH=0x00; OCR3AL=0x00; OCR3BH=0x00; OCR3BL=0x00; OCR3CH=0x00; OCR3CL=0x00; // External Interrupt(s) initialization // INT0: Off // INT1: Off // INT2: Off // INT3: Off // INT4: Off // INT5: Off // INT6: Off // INT7: Off EICRA=0x00; EICRB=0x00; EIMSK=0x00; // Timer/Counter 0 Interrupt(s) initialization TIMSK0=0x02; // Timer/Counter 1 Interrupt(s) initialization TIMSK1=0x00; // Timer/Counter 2 Interrupt(s) initialization TIMSK2=0x00; // Timer/Counter 3 Interrupt(s) initialization TIMSK3=0x00; // USART1 initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART1 Receiver: On // USART1 Transmitter: On // USART1 Mode: Asynchronous // USART1 Baud rate: 500k (Double Speed Mode) <- give linux overruns // USART1 Baud rate: 250k (Double Speed Mode) // USART1 Baud rate: 115200 (Double Speed Mode) //250000 UCSR1A=0x02; UCSR1B=0xD8; UCSR1C=0x06; UBRR1H=0x00; UBRR1L=0x07; //UBRR1L=0x22;//57600 //UBRR1L=0x33;//38400 //UBRR1L=0xCF;//9600 // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; ADCSRB=0x00; // ADC initialization // ADC Clock frequency: 1000,000 kHz // ADC Voltage Reference: AVCC pin // Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On // ADC4: On, ADC5: On, ADC6: On, ADC7: On DIDR0=0x00; ADMUX=FIRST_ADC_INPUT | (ADC_VREF_TYPE & 0xff); ADCSRA=0xCB; // 2 Wire Bus initialization // Generate Acknowledge Pulse: On // 2 Wire Bus Slave Address: 0h // General Call Recognition: On // Bit Rate: 400,000 kHz TWSR=0x00; TWBR=0x02; TWAR=0x01; TWCR=0x45; InitializeCAN(); AddressValidated = 1; // AddressReservationData[0].ManufacturerID = ManufacturerID; // AddressReservationData[0].ProductID = ProductID; // AddressReservationData[0].UniqueIDPerProduct = UniqueIDPerProduct; // AddressReservationData[0].CANAddress = 0x00000001; SetLocalCANAddress(0x00000001); cntUSART1DataOverrun = 0; cntUSART1FramingError = 0; cntUSART1ParityError = 0; cntMambaNetFormatErrorFromSerial = 0; cntTransmitOverrunToUART = 0; #ifdef LCD_CONNECTED SetupLCD(); delay_ms(500); sprintf(DebugString, "Start value %04X%04X", GlobalReceiveSequenceCANAddress>>16, GlobalReceiveSequenceCANAddress&0xFFFF); SetLCDModule(0, 0, DebugString); #endif cntAlive=0; // Global enable interrupts #asm("sei") while (1) { // Place your code here #ifdef LCD_CONNECTED if ((cntMilliSecond-PreviousMilliSecond)>1000) { PreviousMilliSecond = cntMilliSecond; cntAlive++; sprintf(DebugString, "Alive (%d) %04X%04X - S:%02X C:%02X", cntAlive, GlobalReceiveSequenceCANAddress>>16, GlobalReceiveSequenceCANAddress&0xFFFF, Debug_SerialTransmitBufferCount, Debug_CANReceiveBufferCount); SetLCDModule(0, 0, DebugString); sprintf(DebugString, "D%d S%d O%d, M%d O%d R%d T%d ", cntUSART1DataOverrun, cntMambaNetFormatErrorFromSerial, cntTransmitOverrunToUART, cntMambaNetFormatErrorFromCAN, cntMambaNetOverrunFromCAN, cntGlobalCANMessageReceived, cntCANMessageTransmitted); SetLCDModule(0, 1, DebugString); } #endif if (rx_counter1) //while (rx_counter1) { unsigned char ReceivedByte; char cnt; ReceivedByte = getchar1(); switch (ReceivedByte) { case 0xE0: { if (cntReceivedSerialMessageBuffer>10) { cntReceivedSerialMessageBuffer = 0; } } break; default: { switch (cntReceivedSerialMessageBuffer) { case 0: { if (ReceivedByte&0x40) { SerialReceiveMambaNetMessage = 0; } else { SerialReceiveMambaNetMessage = 1; } ToCANAddress = ((unsigned int)ReceivedByte<<7)&0x1F; SequenceNumber = 0; } break; case 1: { ToCANAddress |= ReceivedByte&0x7F; SequenceNumber = 0; } break; case 2: { SequenceNumber = ReceivedByte&0x0F; } break; default: { if (SerialReceiveMambaNetMessage) { FragmentFilled[SequenceNumber] = 1; ReceivedSerialMessageBuffer[(SequenceNumber*8)+cntReceivedSerialMessageBuffer-3] = ReceivedByte; if (ReceivedByte == 0xFF) { char MessageComplete = 1; for (cnt=0; cnt<=SequenceNumber; cnt++) { MessageComplete &= FragmentFilled[cnt]; } if (MessageComplete) SendMambaNetMessageToCAN_DedicatedAddress(ToCANAddress, ReceivedSerialMessageBuffer, ((SequenceNumber*8)+cntReceivedSerialMessageBuffer-3)+1); for (cnt=0; cnt<16; cnt++) { FragmentFilled[cnt] = 0; } } } else { //CAN Control message ReceivedSerialMessageBuffer[cntReceivedSerialMessageBuffer-3] = ReceivedByte; } } break; } cntReceivedSerialMessageBuffer++; } break; case 0xE1: { if (!SerialReceiveMambaNetMessage) { //Parent control message received if (cntReceivedSerialMessageBuffer == 11) { SendCANParentControlMessage(ReceivedSerialMessageBuffer); } } } break; } } // while (UARTTransmitBufferBottom != UARTTransmitBufferTop) if (UARTTransmitBufferBottom != UARTTransmitBufferTop) { char cntTransmitByte; for (cntTransmitByte=0; cntTransmitByte<13; cntTransmitByte++) { putchar1(UARTTransmitBuffer[UARTTransmitBufferBottom][cntTransmitByte]); } UARTTransmitBufferBottom++; if (UARTTransmitBufferBottom>(NR_OF_UART_BUFFERS-1)) { UARTTransmitBufferBottom = 0; } } }
inline void rs485putchar(char c) { putchar1(c); delay_us(1000); }
void getSpeedIndex(void){ char speed_index; speed_index = getIndex(ticks, speed_range, speed_slope, speed_height, speed_max_mem); putchar1(speed_index); //sendData('V', speed_index, TWI_SENSOR_ADDR); }