help with interrupts mikrobasic AVR

Status
Not open for further replies.

martinmc99

Newbie level 1
Joined
Mar 3, 2012
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,287
This code does quite well in receiving serial data and operating the relay drive ports, which have a command for setting and unsetting.
my problem is when then stepper motor attached to portb is set in motion, I cannot operate anything else unless I turn off the power to reset.
I am sure its an interrupt thing, but I do not know where to start with that, so a simple hint or two would be appreciated please.



Code:
program TELEMETRY_RX


dim
 dim n as integer
 dim wipeon as integer
' Bipolar Stepper motor driver connections
dim
    _DIR     as sbit at   PORTb6_bit
    RESET_   as sbit at   PORTb2_bit
    ENABLE_  as sbit at   PORTb0_bit
    _STEP    as sbit at   PORTb1_bit
    SLEEP_   as sbit at   PORTb3_bit
    MS1      as sbit at   PORTb4_bit
    MS2      as sbit at   PORTb5_bit

    _DIR_Direction    as sbit at   DDb6_bit
    RESET_Direction   as sbit at   DDb2_bit
    ENABLE_Direction  as sbit at   DDb0_bit
    _STEP_Direction   as sbit at   DDb1_bit
    SLEEP_Direction   as sbit at   DDb3_bit
    MS1_Direction     as sbit at   DDb4_bit
    MS2_Direction     as sbit at   DDb5_bit
'

dim resolution as byte
dim count as word

dim mystr as char[18]
dim tx as string[2]
dim address as byte
dim drive as integer

sub procedure step_x2()   'FAST SPEED WIPER STEPPER MOTOR
 _STEP = 0
 Delay_us(280)
 _STEP = 1
 Delay_us(280)
end sub

sub procedure Step_x1()    'NORMAL SPEED WIPER STEPPER MOTOR
 _STEP = 0
 Delay_us(350)
 _STEP = 1
 Delay_us(350)
end sub

sub procedure wipe_fast
  if wipeon = 0 then
  exit
  end if
  if wipeon = 2 then
  for n = 0 to 1
  'cw
  _DIR = 0
  ENABLE_ = 0
  for count = 0 to 320 Step_x2() next count
  Delay_ms(3)
  ENABLE_ = 1
  Delay_ms(2)
  'ccw
  _DIR = 1
  ENABLE_ = 0
  for count = 0 to 320 Step_x2() next count
  Delay_ms(3)
  ENABLE_ = 1
  delay_ms(2)
  next n
  end if
end sub

sub procedure intermittant_wipe
if wipeon = 0 then
  exit
  end if
 if wipeon = 3 then
  for n = 0 to 1
  'cw
  _DIR = 0
  ENABLE_ = 0
  for count = 0 to 320 Step_x1() next count
  Delay_ms(400)
  ENABLE_ = 1
  Delay_ms(2)
  'ccw
  _DIR = 1
  ENABLE_ = 0
  for count = 0 to 320 Step_x1() next count
  Delay_ms(400)
  ENABLE_ = 1
  delay_ms(2)
  next n
  delay_ms(5000)
  end if
  
 end sub

sub procedure wipe


  if wipeon = 0 then
  exit
  end if
 if wipeon = 1 then
  for n = 0 to 1
  'cw
  _DIR = 0
  ENABLE_ = 0
  for count = 0 to 320 Step_x1() next count
  Delay_ms(400)
  ENABLE_ = 1
  Delay_ms(2)
  'ccw
  _DIR = 1
  ENABLE_ = 0
  for count = 0 to 320 Step_x1() next count
  Delay_ms(400)
  ENABLE_ = 1
  delay_ms(2)
  next n
  end if
 end sub


 sub procedure relaygo  'READS UART STRING VALUES
  if drive = 1 then

  if mystr[3] = 0x09 then portd.3 = 1
  end if
  if mystr[3] = 0x0b then portd.3 = 0
  end if
  
  'INT WIPE
  if mystr[3] = 0x03  then
  if mystr[5] = 0x4A  then
  wipeon = 0  'INT WIPE OFF
  end if
  end if
  if mystr[3] = 0x07  then
  if mystr[5] = 0x4A  then
  wipeon = 3   'INT WIPE ON
  end if
  end if
  
  'FAST WIPE
  if mystr[3] = 0x03  then
  if mystr[5] = 0x49  then
  wipeon = 0 'FAST WIPE OFF
  end if
  end if
  if mystr[3] = 0x07  then
  if mystr[5] = 0x49  then
  wipeon = 2  'FAST WIPE ON
  end if
  end if

  'WIPE
  if mystr[3] = 0x03  then
  if mystr[5] = 0x46  then
  portd.4 = 0
  wipeon = 0 'WIPE OFF
  end if
  end if
  if mystr[3] = 0x07  then
  if mystr[5] = 0x46  then
  wipeon = 1  'WIPE ON
  portd.4 = 1
  end if
  end if

  if mystr[3] = 0x03  then
  if mystr[5] = 0x47  then
  portd.5 = 0   'RELAY 1 OFF
  end if
  end if

  if mystr[3] = 0x07  then
  if mystr[5] = 0x47  then
  portd.5 = 1   'RELAY 1 ON
  end if
  end if

  if mystr[3] = 0x03  then
  if mystr[5] = 0x48  then
  portd.6 = 0   'RELAY 2 OFF
  end if
  end if

  if mystr[3] = 0x07  then
  if mystr[5] = 0x48  then
  portd.6 = 1   'RELAY 2 ON
  end if
  end if


  'ACK FUCNTION GLOBAL RELAY OFF AND WIPE OFF
  if mystr[3] = 0x21 then portd.6 = 0
  end if
  if mystr[3] = 0x21 then portd.5 = 0
  end if
  if mystr[3] = 0x21 then portd.4 = 0
  end if
  if mystr[3] = 0x21 then wipeon = 0
  end if
  end if
 end sub


sub procedure relaydrive()
  drive = 0
  address = 0
  'READ DEVICE ADDRESS PORT
  if PINa.0 = 1 then address = address + 1
  else address = address
  end if
  if PINa.1 = 1 then address = address + 2
   else address = address
  end if
  if PINa.2 = 1 then address = address + 4
   else address = address
  end if
  if PINa.3 = 1 then address = address + 8
   else address = address
  end if
  if PINa.4 = 1 then address = address + 16
   else address = address
  end if
  if PINa.5 = 1 then address = address + 32
  else address = address
   end if
  if PINa.6 = 1 then address = address + 64
   else address = address
  end if
  if PINa.7 = 1 then address = address + 128
   else address = address
  end if
  if mystr[1] = address then  drive = 1
  end if


end sub
'sub procedure UART1_INT()

 'main
 
'end sub






sub procedure  ReadComStr

  dim cnt as byte          ' counter for character array(string)
  dim read as boolean      ' loop status
  dim timeout as word
  portd.2 = 0
  cnt = 0                '
  read = true
  timeout = 0

  While read

      If UART1_DATA_READY = 1 Then                     ' character available?
         mystr[cnt]  = UART1_READ                      ' get character and load string
         If mystr[cnt] = 0xaf Then read = false End If    ' if delimeter character then get out of loop
         inc(cnt)                                      ' prepare for next string character
         timeout = 0
                                            ' reset no-read timer
      End If
       If UART1_DATA_READY = 1 Then portd.2 = 1
      end if
      If (inc(timeout) = 500) or (cnt = 8) Then read = false End If  ' if no-read timer reaches a few milliseconds

   Wend                                                            ' or character limit is reached then abort

end sub

main:


UART1_INIT(4800)

Delay_ms(100)
'UCSRB.B1 = 1

'SREG.B7 = 1
wipeon = 0

  DDRd.2 = 1 ' Set PORTd.2 as output
  DDRd.3 = 1 ' Set PORTd.3 as output
  DDRd.4 = 1 ' Set PORTd.4 as output
  DDRd.5 = 1 ' Set PORTd.5 as output
  DDRd.6 = 1 ' Set PORTd.6 as output

  DDRB.0 = 0 ' Set PORTB.7 as input
  DDRB.1 = 0 ' Set PORTB.7 as input
  DDRB.2 = 0 ' Set PORTB.7 as input
  DDRB.3 = 0 ' Set PORTB.7 as input
  DDRB.4 = 0 ' Set PORTB.7 as input
  DDRB.5 = 0 ' Set PORTB.7 as input
  DDRB.6 = 0 ' Set PORTB.7 as input
  DDRB.7 = 0 ' Set PORTB.7 as input
  
  address = 0
  count = 0
  resolution = 1
  _DIR = 0
  RESET_ = 1
  ENABLE_ = 1
  _STEP = 0
  SLEEP_ = 1
  MS1 = 1
  MS2 = 1

  _DIR_Direction = 1
  RESET_Direction = 1
  ENABLE_Direction = 1
  _STEP_Direction = 1
  SLEEP_Direction = 1
  MS1_Direction = 1
  MS2_Direction = 1

While true

   If UART1_DATA_READY = 1 Then ReadComStr
   End If
   relaydrive
   relaygo
   wipe
   wipe_fast
   intermittant_wipe

   
Wend




end.
 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…