Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Locomotive controller with Bluetooth HC-05

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
rafael_jimenez

Bascom Member



Joined: 26 Oct 2006
Posts: 27
Location: Wesseling

dominicanrep.gif
PostPosted: Wed Nov 20, 2019 11:20 am    Post subject: Locomotive controller with Bluetooth HC-05 Reply with quote

Hello friends of the forum,
I'm testing a locomotive with a controller that works with 3.5 volts.

My program works as it is, but I want PWM signals to be controlled faster.

I made an application with MIT App inventor,

could someone please help me the best way to make PWM signals more controllable?

your collaboration is appreciated

[/code]
'*******************************************************************************
'** Electric Locomotive Version 1.0 *
'** PORTC 1,2,3,4,5 = Relay
'** PORTC.0 = Led Signal monitor
'** PORTB.2 = PWM for Motor driver
'** Bascom version 2.0,8,1
'**___________________________________________________________________________
$regfile = "m8def.dat"
$crystal = 16000000 '16000000
$hwstack = 80
$swstack = 80
$framesize = 80
$baud = 9600


Dim A As String * 2
Dim Pwmval As Byte

Config Portb.0 = Output 'Signal OK
Config Portc.0 = Output ' Main switch
Config Portc.1 = Output 'Emergency Motor Batt. on/Off
Config Portc.2 = Output 'Direction
Config Portc.3 = Output 'Licht
Config Portc.4 = Output ' Horn
Config Portc.5 = Output


Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare B Pwm = Clear Up ' , Compare A Pwm = Clear Up


'Start Timer1
'Enable Interrupts
Led_blink Alias Portb.0 'led blink Microcontroller loop
Button_1 Alias Portc.0 'relay 1
Button_2 Alias Portc.1 'relay 2
Button_3 Alias Portc.2 'relay 3
Button_4 Alias Portc.3 'Relay 4
Button_5 Alias Portc.4 'relay 5


'Ocr1b = 0
Pwmval = 0

Do
Toggle Portb.0
Waitms 40


If Ischarwaiting() = 1 Then
A = Inkey()



'Main Switch For Modul

If A = "1" Then
If Button_1 = 1 Then 'Master Switch
Button_1 = 0
Print "1_ON"
Else
Button_1 = 1
Print "1_OFF"
End If
End If


'Button Switch Modul
If A = "2" Then
If Button_2 = 1 Then 'If Button_2 = 0 Then ' motor schaalter Bat.
Button_2 = 0 ' Button_2 = 1
Print "2_ON" ' Print "2_ON"
Else 'Else
Button_2 = 1 'Button_2 = 0
Print "2_OFF" 'Print "2_OFF"

End If
End If



'Button for Direction
If A = "3" Then

If Pwmval <> 0 Then
Pwmval = 0 ' set motor to cero
Ocr1b = Pwmval
Waitms 500
End If

If Button_3 = 1 Then ' direction change
Button_3 = 0
Print "3_ON"
Else

Button_3 = 1
Print "3_OFF"

End If
End If




'Button for Licht
If A = "4" Then
If Button_4 = 1 Then
Button_4 = 0
Print "4_ON"
Else
Button_4 = 1
Print "4_OFF"
End If


End If
' Button for Horn
If A = "5" Then
Toggle Button_5
'Button_5 = 1
Waitms 500
Reset Button_5
'Button_5 = 0

End If

'__________________________________
'**** motor control Maxim 3,5.4volt Output

If A = "6" Then

Start Timer1 'timer1 loop

If Pwmval < 192 Then Incr Pwmval 'Fader Motor Up
'Waitms 100 :
Ocr1b = Pwmval '+ 20

End If


If A = "7" Then

If Pwmval > 0 Then Decr Pwmval 'Fader Motor down
' Waitms 50 :
Ocr1b = Pwmval + 10

End If
End If

Loop



End
Code:
 


(BASCOM-AVR version : 2,0,8,1 , Latest : 2.0.8.2 )
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 624

poland.gif
PostPosted: Wed Nov 20, 2019 12:39 pm    Post subject: Reply with quote

I think that I tweak some of your serial receiving routines because buffered serial and moving 40ms delay into Timer2 control but Im thinking that some of the rest of code can be better.

Code:
'*******************************************************************************
'** Electric Locomotive Version 1.0 *
'** PORTC 1,2,3,4,5 = Relay
'** PORTC.0 = Led Signal monitor
'** PORTB.2 = PWM for Motor driver
'** Bascom version 2.0,8,1
'**___________________________________________________________________________
$regfile = "m8def.dat"
$crystal = 16000000 '16000000
$hwstack = 80
$swstack = 80
$framesize = 80
$baud = 9600


Dim A As String * 2
Dim Pwmval As Byte

Config Portb.0 = Output 'Signal OK
Config Portc.0 = Output ' Main switch
Config Portc.1 = Output 'Emergency Motor Batt. on/Off
Config Portc.2 = Output 'Direction
Config Portc.3 = Output 'Licht
Config Portc.4 = Output ' Horn
Config Portc.5 = Output


Config Timer1 = Pwm , Pwm = 8 , Prescale = 1 , Compare_b_pwm = Clear_up

Led_blink Alias Portb.0 'led blink Microcontroller loop
Button_1 Alias Portc.0 'relay 1
Button_2 Alias Portc.1 'relay 2
Button_3 Alias Portc.2                                      'relay 3
Button_4 Alias Portc.3 'Relay 4
Button_5 Alias Portc.4                                      'relay 5


Pwmval = 0


'************************************************
'*         CONFIG TIMER2 FOR TIME BASE          *
'************************************************
 Config Timer2 = Timer , Prescale = 1024 , Clear_timer = 1
  Compare2 = 155                                            '10ms @16MHz/1024

  Dim Miliseconds As Byte
'************************************************
'*       CONFIG USART FOR BACKGROUND RX         *
'************************************************
 Config Serialin0 = Buffered , Size = 10

 Enable Interrupts                                          'needed for buffered serial

Do

   '10ms
   If Tifr.ocf2 = 1 Then                                    'timer sets flag
    Tifr.ocf2 = 1                                           'clearing flag by writing one

      If Miliseconds < 39 Then                              '40ms
       Incr Miliseconds
      Else
       Miliseconds = 0
       Toggle Portb.0
      End If

   End If

   If Ischarwaiting() = 1 Then
      A = Inkey()


      Select Case A

       Case "1"

        'Main Switch For Modul
         If Button_1 = 1 Then                               'Master Switch
            Button_1 = 0
            Print "1_ON"
         Else
            Button_1 = 1
            Print "1_OFF"
         End If

       Case "2"

         'Button Switch Modul
         If Button_2 = 1 Then                               'If Button_2 = 0 Then ' motor schaalter Bat.
            Button_2 = 0                                    ' Button_2 = 1
            Print "2_ON"                                    ' Print "2_ON"
         Else                                               'Else
            Button_2 = 1                                    'Button_2 = 0
            Print "2_OFF"                                   'Print "2_OFF"

         End If

       Case "3"

         'Button for Direction
         If Pwmval <> 0 Then
            Pwmval = 0 ' set motor to cero
            Ocr1b = Pwmval
            Waitms 500
         End If

         If Button_3 = 1 Then ' direction change
            Button_3 = 0
            Print "3_ON"
         Else

            Button_3 = 1
            Print "3_OFF"

         End If

       Case "4"

         'Button for Licht
         If Button_4 = 1 Then
            Button_4 = 0
            Print "4_ON"
         Else
            Button_4 = 1
            Print "4_OFF"
         End If

       Case "5"

         ' Button for Horn
        If A = "5" Then
         Toggle Button_5
          'Button_5 = 1
         Waitms 500
         Reset Button_5
          'Button_5 = 0

        End If

       Case "6"

        If A = "6" Then

         Start Timer1 'timer1 loop

         If Pwmval < 192 Then Incr Pwmval 'Fader Motor Up
          'Waitms 100 :
         Ocr1b = Pwmval '+ 20

        End If

       Case "7"

        If A = "7" Then

         If Pwmval > 0 Then Decr Pwmval 'Fader Motor down
          ' Waitms 50 :
         Ocr1b = Pwmval + 10

        End If

      End Select


   End If

Loop



End


Have a nice day Wink
Back to top
View user's profile Visit poster's website
rafael_jimenez

Bascom Member



Joined: 26 Oct 2006
Posts: 27
Location: Wesseling

dominicanrep.gif
PostPosted: Thu Nov 28, 2019 7:29 pm    Post subject: Reply with quote

Hello EDC
I'm sorry, but I am in the Hospital where I have had kidney stones, I plan to be home this weekend.
Thank you very much for your answer
Regards
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum