×
单片机 > 单片机程序设计 > 详情

单片机数控云台master 仿真及源程序

发布时间:2020-06-18 发布时间:
|

源程序:

/*********************************************

This program was produced by the

CodeWizardAVR V1.23.8c Standard

Automatic Program Generator

?Copyright 1998-2003 HP InfoTech s.r.l.

http://www.hpinfotech.ro

e-mail:office@hpinfotech.ro


Project : SKYT

Version : 0.01

Date    : 2006-12-6

Author  : hlchen                          

Company :                                 

Comments:   

数控云台 

Chip type           : ATmega8 

Program type        : Application

Clock frequency     : 3.690000 MHz

Memory model        : Small 

External SRAM size  : 0 

Data Stack size     : 256 

*********************************************/

typedef unsigned char uchar;  

typedef unsigned int uint;

#include   

#include  

#include "macro.h"

#include "LCD.h" 

//#include "sed1565_s.h"

#define RXB8 1

#define TXB8 0

#define UPE 2

#define OVR 3

#define FE 4

#define UDRE 5

#define RXC 7

#define Light 5

#define FRAMING_ERROR (1<

#define PARITY_ERROR (1<

#define DATA_OVERRUN (1<

#define DATA_REGISTER_EMPTY (1<

#define RX_COMPLETE (1<

#define sysInitLcd 0

// USART Receiver buffer

#define RX_BUFFER_SIZE 8

char rx_buffer[RX_BUFFER_SIZE];

unsigned char rx_wr_index,rx_rd_index,rx_counter;

// This flag is set on USART Receiver buffer overflow

bit rx_buffer_overflow,b_ComEnable;

uchar uc_syssta;

// USART Receiver interrupt service routine

#pragma savereg-

interrupt [USART_RXC] void uart_rx_isr(void)

char status,data;

#asm

    push r26

    push r27

    push r30

    push r31

    in   r26,sreg 

    push r26

#endasm

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;

   if (++rx_counter == RX_BUFFER_SIZE)

      {

      rx_counter=0;

      rx_buffer_overflow=1;

      };

   };

if ((rx_buffer[0]=='o')&&(rx_buffer[1]=='k')) b_ComEnable=1; 

#asm

    pop  r26

    out  sreg,r26

    pop  r31

    pop  r30

    pop  r27

    pop  r26

#endasm

}

#pragma savereg+


#ifndef _DEBUG_TERMINAL_IO_

// Get a character from the USART Receiver buffer

#define _ALTERNATE_GETCHAR_

#pragma used+

char getchar(void)

{

char data;

while (rx_counter==0);

data=rx_buffer[rx_rd_index];

if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;

#asm("cli")

--rx_counter;

#asm("sei")

return data;

}

#pragma used-

#endif


// USART Transmitter buffer

#define TX_BUFFER_SIZE 8

char tx_buffer[TX_BUFFER_SIZE];

unsigned char tx_wr_index,tx_rd_index,tx_counter;


// USART Transmitter interrupt service routine

#pragma savereg-

interrupt [USART_TXC] void uart_tx_isr(void)

{

#asm

    push r26

    push r27

    push r30

    push r31

    in   r26,sreg

    push r26

#endasm

if (tx_counter)

   {

   --tx_counter;

   UDR=tx_buffer[tx_rd_index];

   if (++tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0;

   }; 


#asm

    pop  r26

    out  sreg,r26

    pop  r31

    pop  r30

    pop  r27

    pop  r26

#endasm

}

#pragma savereg+


#ifndef _DEBUG_TERMINAL_IO_

// Write a character to the USART Transmitter buffer

#define _ALTERNATE_PUTCHAR_

#pragma used+

void putchar(char c)

{

while (tx_counter == TX_BUFFER_SIZE);

#asm("cli")

if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0))

   {

   tx_buffer[tx_wr_index]=c;

   if (++tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0;

   ++tx_counter;

   }

else UDR=c;

#asm("sei")

}

#pragma used-

#endif

// Standard Input/Output functions

#include  

uchar ucInputValue;

uint uiLastADValue[3];

uchar ucInputParam0; 

uchar ucChannelN,uc_ReceiveSta;

bit bInputChange,bMotorSpeed,bLight,bReceiveOpend;         

void inputdeal(void); 

void TransmitEnable(void);

void ReceiveEnable(void); 

void Communication(void);

void LcdShow(void);

#define HighSpeed 0x33

#define SlowSpeed 0x99

#define ADC_VREF_TYPE 0x00      

#define RE 0

#define DE 1

// ADC interrupt service routine

interrupt [TIM0_OVF] void timer0_ovf_isr(void)

{

// Place your code here 

TCNT0=0xf0;

}

interrupt [ADC_INT] void adc_isr(void)

{

uint adc_data;

// Read the AD conversion result

adc_data=ADCW;


// Place your code here 

switch (ucChannelN)

{

case 0:

                       if (uiLastADValue[0]!=adc_data)

                {

                uiLastADValue[0]=adc_data;

                ucInputParam0=uiLastADValue[0]*24/1024;

                //ucInputParam0 = tmp/100;

                //ucInputParam1 = (tmp%100)/10;

                //ucInputParam2 = (tmp%100)%10;

                bInputChange=1;

                ucInputValue=0x01;

               

                } 

                ADMUX=0x01;

                break;  

case 1: 

                if (uiLastADValue[1]!=adc_data)

                {

                uiLastADValue[1]=adc_data;

                ucInputParam0=uiLastADValue[1]*12/1024;

                //ucInputParam0 = tmp/100;

                //ucInputParam1 = (tmp%100)/10;

                //ucInputParam2 = (tmp%100)%10;

                bInputChange=1;

                ucInputValue=0x02; 

             

                } 

                ADMUX=0x02;

                break;  

case 2:

                if (uiLastADValue[2]!=adc_data)

                {

                uiLastADValue[2]=adc_data;

                //ucInputParam0=uiLastADValue[2]*6/1024; 

                       if (abs(uiLastADValue[2]-1023)<100)

                                ucInputValue=3;

                   else if (abs(uiLastADValue[2]-465)<50)

                                ucInputValue=4;

                       else if (abs(uiLastADValue[2]-292)<25)

                                ucInputValue=5;         

                       else if (abs(uiLastADValue[2]-204)<25)

                                ucInputValue=6;

                       else if (abs(uiLastADValue[2]-146)<12)

                                ucInputValue=7;

                       else if (abs(uiLastADValue[2]-93)<12) 

                                ucInputValue=8;

                       else if (abs(uiLastADValue[2])<6)

                                ucInputValue=9;

                       else break;

               //        else if (abs(uiLastADValue[2]-512

                //ucInputParam0 = tmp/100;

                //ucInputParam1 = (tmp%100)/10;

                //ucInputParam2 = (tmp%100)%10;

                bInputChange=1;

//                ucInputValue=ucInputParam0+2; 

             

                } 

                ADMUX=0x00;

                break;              

default:



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
STM32中断向量表的位置.重定向