📄 picservo.lst
字号:
MPASM 4.02 Released PICSERVO.ASM 12-31-2006 11:50:00 PAGE 1
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00001 LIST P=PIC16F84, R=DEC
00002 #INCLUDE "p16f84.inc"
00001 LIST
00002 ; P16F84.INC Standard Header File, Version 2.00 Microchip Technology, Inc.
00136 LIST
00003
00004 ;------------------------------------------------------------------------------
00005 ; ASSEMBLE With MPASM. available for free from http://www.microchip.com
00006 ;------------------------------------------------------------------------------
00007 ;
00008 ; Servo Controler Version 1.1
00009 ; 28/10/2001
00010 ; - Fixed Serial receive data problem where the PIC could
00011 ; miss a byte by assuming that the PC isn't in the middle of
00012 ; sending a byte as it deactivates CTS.
00013 ;
00014 ; This program will control 8 servos connected to PORT B of the
00015 ; PIC microcontroler.
00016 ;
00017 ; The servos work by responding to a pulse presented to them
00018 ; approximately every 20ms. The width of the pulse controls the
00019 ; position of the servo.
00020 ;
00021 ; In general, a pulse width of around 1520us is used as a neutral
00022 ; (centre) position indication. The pulse width is then increased
00023 ; or decreased from there. Generally it the pulse range goes from
00024 ; 1 to 2 ms for full deflection (90 degree movement of horn.
00025 ;
00026 ; So we need to arrange timing to control the width of the output
00027 ; pulse. To do this we will run the PIC at 4MHz, this means that
00028 ; our instructions are 1us long (clock/4). We will arrange a delay
00029 ; loop to wait for a given number of clock cycles.
00030 ;
00031 ; Because servo aren't that accurate, we will divide the 1 to 2ms
00032 ; pulse range into 256 values, which if we work on a loop of
00033 ; four instruction cycles we can get a loop between 4 and 1024 ms
00034 ; (the loop is constructed to run at least once)
00035 ;
00036 ; We then need to offset this (roughly) 0 to 1ms pulse to ensure
00037 ; that the servo is centered. We will do this by having an "offset"
00038 ; adjustment for each servo. This offset will control the width of
00039 ; the initial part of the pulse from 4 to 1024ms. This means that
00040 ; we can centre the servo using the offset and then move the servo
00041 ; +/-45 degrees from there. With careful use of the offset and
00042 ; position, it should be possible to use almost the full travel of
00043 ; the servo.
00044 ;
00045 ; We will also need to be able to turn on and off the output of
00046 ; each servo driver.
00047 ;
00048 ; PORT B is configured:
00049 ;
00050 ; RB0 Servo Control 0
MPASM 4.02 Released PICSERVO.ASM 12-31-2006 11:50:00 PAGE 2
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00051 ; RB1 Servo Control 1
00052 ; RB2 Servo Control 2
00053 ; RB3 Servo Control 3
00054 ; RB4 Servo Control 4
00055 ; RB5 Servo Control 5
00056 ; RB6 Servo Control 6
00057 ; RB7 Servo Control 7
00058 ;
00059 ; PORT A is configured:
00060 ;
00061 ; RA0 Serial TX (output)
00062 ; RA1 Serial Request To Send (input)
00063 ; RA2 Serial Clear To Send (output)
00064 ; RA3 Serial RX (input)
00065 ; RA4 LED Drive (0=on, 1=off. Used to indicate data transmission)
00066 ;
00067 ; Control Data
00068 ;
00069 ; To control the servos and outputs we need to send commands to the PIC.
00070 ; These commands will come from the serial port of a host computer running
00071 ; at 2400 baud, Hardware flow control (using CTS/RTS) with eight data bits,
00072 ; one stop bit and no parity. (2400 8-N-1).
00073 ;
00074 ; The commands take the format of one or two bytes sent sequentially
00075 ; the first being the command to execute the second containing the data
00076 ; for the command if needed.
00077 ;
00078 ; The command byte is split into two "nibbles". The upper 4 bits are used to
00079 ; determine the command, the lower 4 bits are used to select the output channel
00080 ;
00081 ; +-----+-----+-----+-----+-----+-----+-----+-----+
00082 ; MSB | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | LSB
00083 ; | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | Decimal Value
00084 ; +-----+-----+-----+-----+-----+-----+-----+-----+
00085 ; | Command | Channel Select |
00086 ; +-----+-----+-----+-----+-----+-----+-----+-----+
00087 ;
00088 ; Note that the value (in hex or decimal) is added to the channel number
00089 ; to generate the command byte.
00090 ;
00091 ; The Commands are:
00092 ;
00093 ; Hex Decimal Meaning
00094 ; ---------------------------------------------------------------------------
00095 ; 0x00 0 Reset Device. All outputs off, servos disabled and
00096 ; offset and position values set to 128 (mid range).
00097 ; Data byte MUST BE Zero. The Channel Value MUST BE Zero.
00098 ; Special, SHOULD be followed by another zero byte, see
00099 ; text below.
00100 ;
00101 ; 0x10 16 Set Servo Output "Positon Value". Data byte is the
00102 ; value (between 0 and 255). The Channel Value must be
00103 ; set in the lower 4 bits between 0 and 7.
MPASM 4.02 Released PICSERVO.ASM 12-31-2006 11:50:00 PAGE 3
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00104 ;
00105 ; 0x20 32 Set Servo Output "Offset Value". Data byte is the
00106 ; value (between 0 and 255). The Channel Value must be
00107 ; set in the lower 4 bits between 0 and 7.
00108 ;
00109 ; 0x30 48 Enable Servo Output. This starts the PIC generating
00110 ; servo control pulses on the given channel. The Channel
00111 ; Value must be set in the lower 4 bits between 0 and 7.
00112 ; No Data byte.
00113 ;
00114 ; 0x40 64 Disable Servo Output. This stops the PIC generating
00115 ; servo control pulses on the given channel. The Channel
00116 ; Value must be set in the lower 4 bits between 0 and 7.
00117 ; No Data byte.
00118 ;
00119 ;
00120 ; Parser State Control.
00121 ;
00122 ; Even with the hardware flow control where the PIC uses the CTS line to inhibit
00123 ; the host from sending more data when it can't receive it, there is a chance
00124 ; the host and PIC will get out of step with commands and data bytes. This could
00125 ; occur because of either a bug in the host software or power being lost to the
00126 ; PIC. This may result in the PIC attempting to treat a data byte as a command or
00127 ; a command as a data byte. Therefore some scheme is needed to ensure that the
00128 ; state of both can be quickly resynchronised.
00129 ;
00130 ; This will be done in two ways, the reset command and the rejection of unknown
00131 ; commands.
00132 ;
00133 ; If the PIC receives a byte it believes to be a command but is doesn't
00134 ; recognise the command, it will discard it and start looking for another
00135 ; command byte. This means that for more values of the data bytes it will
00136 ; ignore these as commands. Unfortunately not all however. It is therefore
00137 ; possible for the PIC to execute incorrent commands until it comes accross
00138 ; one that doesn't make sense. This means that it could skip a normal reset
00139 ; reset command if it was one byte long by reading it as a data byte.
00140 ;
00141 ; The reset command is two bytes long (a command and a data byte) both being
00142 ; zeros. However to ensure that the PIC is reset EVERY TIME the resent command
00143 ; it sent, it should be sent as THREE sequential zero bytes.
00144 ;
00145 ; The reason is that if the PIC is out of step and waiting for a data byte, it
00146 ; will comsume the first zero as a data byte, take the next one as the command
00147 ; and the last one as the data byte. But what if it isn't out of sync? The
00148 ; parser in the PIC will know that a zero command requires a zero data byte
00149 ; to be valid, therefore it will receive the first zero as the command, the
00150 ; second as the data and execute the reset. It will then get another zero byte
00151 ; which is the command for reset and be waiting for the corresponding zero data
00152 ; byte. However the host will start sending other valid command at this time
00153 ; and the parser will not receive a zero data byte. It then knows that it must
00154 ; discard the reset command and use the new byte as a command.
00155 ;
00156
MPASM 4.02 Released PICSERVO.ASM 12-31-2006 11:50:00 PAGE 4
LOC OBJECT CODE LINE SOURCE TEXT
VALUE
00157 ;+=============================================================================
00158 ;| Program Beginning.
00159 ;+=============================================================================
00160
00161 ; Set the configuration, PUT Enabled, WDT Disabled, XT Oscillator, No Code Protection
00162 ;
2007 3FF1 00163 __CONFIG _XT_OSC & _PWRTE_ON & _WDT_OFF & _CP_OFF
00164
00165
00166 ;+-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -