Код: Выделить всё
#include <mega32>
#include <stdio>
#include <delay>
//#define LEDR PORTD.4
//#define LEDG PORTD.5
//#define LEDB PORTD.3
// 1 Wire Bus functions
#asm
.equ __w1_port=0x18 ;PORTB
.equ __w1_bit=7
#endasm
#include <1wire>
// DS1820 Temperature Sensor functions
#include <ds18b20>
bit n;
char input, output, temp;
char k=1, u;
// maximum number of DS1820 devices
// connected to the 1 Wire bus
#define MAX_DS18B20 2
// number of DS1820 devices
// connected to the 1 Wire bus
unsigned char ds18b20_devices;
// DS1820 devices ROM code storage area,
// 9 bytes are used for each device
// (see the w1_search function description in the help)
unsigned char rom_codes[MAX_DS18B20][9];
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 3
char rx_buffer[RX_BUFFER_SIZE];
char rx_ready=1;
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_counter;//rx_rd_index,
#else
unsigned int rx_wr_index,rx_counter;//rx_rd_index,
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE)
{
rx_wr_index=0;
rx_ready=0;
}
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
}
#define ADC_VREF_TYPE 0xE0
// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;
}
void init_cpu(void)
{
// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0xFF;
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=In Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=T State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0xEF;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=In
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=T
PORTD=0x00;
DDRD=0xFE;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 500,000 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x02;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 500,000 kHz
// ADC Voltage Reference: Int., cap. on AREF
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x83;
// Determine the number of DS1820 devices
// connected to the 1 Wire bus
ds18b20_devices=w1_search(0xf0,rom_codes);
// Global enable interrupts
#asm("sei")
}
//
//unsigned char crc8(unsigned char *Ptr, unsigned char len)
//{
// unsigned int crc = 0x00;
// unsigned int i;
//
// while (len--)
// {
// crc^=*Ptr++; //*Ptr - значение переменной по адресу
// for (i=0;i<8;i++)
// {
// if(crc & 0x80) crc=(crc<<1)^0x31;
// else crc = crc << 1;
// };
// };
// return crc;
//}
void izm_input(void)
{
//printf("Work 222");
if(rx_buffer[1]==0x31)
{
char i;
temp=0;
input=0;
for(i=0;i<8;i++)
{
PORTC=(i*2);
temp|=(PINC.4<<i);
};
if(temp&0b00001000) input|=1<<0;
if(temp&0b00000001) input|=1<<1;
if(temp&0b00000010) input|=1<<2;
if(temp&0b00000100) input|=1<<3;
if(temp&0b00010000) input|=1<<4;
if(temp&0b01000000) input|=1<<5;
if(temp&0b10000000) input|=1<<6;
if(temp&0b00100000) input|=1<<7>-1;ii--)
{
PORTA.3=0;//(output&(1<<i));
PORTA.1=1;
PORTA.1=0;
};
PORTA.2=0;
PORTA.2=1;
rx_ready=1;
while (1)
{
n=0;
while(rx_ready); // Ожидание внешнего запроса с COM порта.
rx_ready=1;
// upr_output();
//printf("Hello, World!!!");
// crc=crc8(rx_buffer,3); // Контроль CRC
// if (rx_buffer[3]==crc)
{
//delay_ms(300); // Просто пауза :)
switch(rx_buffer[0])
{
default: break;
case 0x31: izm_napr(); break;// // Вход в режим измерения напряжение.
case 0x32: izm_input(); break;// // Вход в режим опроса входов.
case 0x33: upr_output(); break;// // Вход в режим управления выходами.
case 0x34: izm_temp(); break;// // Вход в режим измерения температуры.
case 0x35: upr_led(); break;// putchar(n); // Вход в режим управления RGB
case 0x36: light(); break;// // Вход в режим "вежливой" подсветки
};
// if(n) putchar(n); // Вывод результата в COM порт.
// putchar(0x33);
};
};
};