Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Interface between MPP solar products & PC
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1192
Location: SYDNEY

australia.gif
PostPosted: Fri Jan 22, 2016 5:48 am    Post subject: Interface between MPP solar products & PC Reply with quote

This code makes an interface between MPP solar products and a terminal running on a PC
You type the command in the terminal then press enter
The AVR calculates the CRC and adds it to the command then sends it to the MPP solar inverter
The MPP solar inverter responds with some data which is then sent to the terminal on the PC

This is just the first step more to come!

Regards Paul
Code:


  'Interface between MPP solar products and PC terminal



  $regfile = "m2560def.dat"                                 ' specify the used micro


$crystal = 16000000                                         ' used crystal frequency
$baud = 57600                                               ' use baud rate
$hwstack = 50
$swstack = 50
$framesize = 50


 Dim Alen As Byte
Dim Onlongs(17) As Long                                     'numbers to sort Long, Word, Byte as needed
'Dim X As Byte


Dim Thecrc As Word
Dim Rs232 As Byte
Dim Uart_command_in As String * 50
Dim Uart_command As String * 254
Dim Send_command As String * 50
Dim Send_bytes(49) As Byte At Send_command Overlay
Dim Crcstr As String * 2
Dim Crcwrd As Word At Crcstr Overlay
Dim Z As Byte






Config Com2 = 2400 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Open "com2:" For Binary As #1

Do

  Do
   If Ischarwaiting() = 1 Then
      Rs232 = Inkey()                                       'get a character from the buffer

      If Rs232 = 13 Or Len(uart_command_in) > 49 Then       'uart_command full or enter key sent
              Print
           Exit Do

      Else
         Uart_command_in = Uart_command_in + Chr(rs232)     'add the character to the uart_command buffer
         Print Chr(rs232) ;
      End If
   End If

  Loop

      Send_command = Uart_command_in
      Uart_command_in = ""

     Send_command = Ucase(send_command)    

  Z = Len(send_command)

  Thecrc = Crc16(send_bytes() , Z)                          'workout the CRC
  Swap Thecrc
  'Print Hex(thecrc)
  Crcwrd = Thecrc


    Print #1 , Send_command ; Crcstr ; Chr(13) ;

  Do
   If Ischarwaiting(#1) = 1 Then
      Rs232 = Inkey(#1)                                     'get a character from the buffer

      If Rs232 = 13 Or Len(uart_command) > 253 Then         'uart_command full or enter key sent

            Print Uart_command
             Uart_command = ""
         Exit Do
      Else
         Uart_command = Uart_command + Chr(rs232)           'add the character to the uart_command buffer
         
      End If
   End If

  Loop


Loop

 End                                                        'end program

 
Back to top
View user's profile
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Fri Jan 22, 2016 7:59 am    Post subject: Terminal program Reply with quote

Nice terminal program with extras.

Have fun
Ben Zijlstra
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1192
Location: SYDNEY

australia.gif
PostPosted: Sun Jan 24, 2016 11:43 am    Post subject: Reply with quote

Now the CRC16 is calculated both ways send and receive
So if you hook two AVRs together via rs232 you can check the data is valid

Regards Paul

Code:





  'Interface between MPP solar products and PC terminal



  $regfile = "m2560def.dat"                                 ' specify the used micro


$crystal = 16000000                                         ' used crystal frequency
'$baud = 57600                                               ' use baud rate
$hwstack = 50
$swstack = 50
$framesize = 50


 Dim Alen As Byte
Dim Onlongs(17) As Long                                     'numbers to sort Long, Word, Byte as needed
'Dim X As Byte



Dim Crc_16 As String * 2
Dim Work2 As Word At Crc_16 Overlay
Dim Work(35) As String * 40
Dim Thecrc As Word
Dim Rs232 As Byte
Dim Uart_command_in As String * 50
Dim Uart_command As String * 254
Dim Aruart_command(253) As Byte At Uart_command Overlay
Dim Send_command As String * 50
Dim Send_bytes(49) As Byte At Send_command Overlay
Dim Crcstr As String * 2
Dim Crcwrd As Word At Crcstr Overlay
Dim Z As Byte
Dim Task As Byte


Declare Sub Chopup
Declare Sub Find_command
Declare Sub Cal_crc16


Config Com1 = 57600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Config Com2 = 2400 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Open "com2:" For Binary As #1

Do
     Task = 0
  Do
   If Ischarwaiting() = 1 Then
      Rs232 = Inkey()                                       'get a character from the buffer

      If Rs232 = 13 Or Len(uart_command_in) > 49 Then       'uart_command full or enter key sent


               Print
               Uart_command_in = Ucase(uart_command_in)     'make uper case


           Exit Do

      Else
         Uart_command_in = Uart_command_in + Chr(rs232)     'add the character to the uart_command buffer
         Print Chr(rs232) ;
      End If
   End If

  Loop

   Select Case Uart_command_in

               Case "INFO"                                  'get the state of inverter
                   Uart_command_in = "QPIGS"
                   Task = 1

   End Select

      Send_command = Uart_command_in
      Uart_command_in = ""

    Z = Len(send_command)

  Thecrc = Crc16(send_bytes() , Z)                          'workout the CRC
  Swap Thecrc
  'Print Hex(thecrc)
  Crcwrd = Thecrc


    Print #1 , Send_command ; Crcstr ; Chr(13) ;

  Do
   If Ischarwaiting(#1) = 1 Then
      Rs232 = Inkey(#1)                                     'get a character from the buffer

      If Rs232 = 13 Or Len(uart_command) > 253 Then         'uart_command full or enter key sent
             Delchars Uart_command ,(13)
             Call Find_command

            Print "RETURNED DATA>" ; Uart_command
             Uart_command = ""
         Exit Do
      Else
         Uart_command = Uart_command + Chr(rs232)           'add the character to the uart_command buffer

      End If
   End If

  Loop


Loop

 End                                                        'end program





 Sub Find_command
     Local A As String * 3
     Local B As Byte
     Local C As Byte
      Local D As Byte
      Local E As Long
     C = &B00000111

   Call Chopup
   Select Case Task
                    Print "Status bits>" ; Work(17)
               Case 1                                       'get the state of inverter
                   Delchar Work(1) , 1                      'remove the (
                   'work(17)      staus bits

                   Print ">>Inverter Stats<<"
                   Print "Grid Volts=" ; Work(1)
                   Print "Grid Frequency=" ; Work(2)
                   Print "AC Output Volts=" ; Work(3)
                   Print "AC Output Frequency" ; Work(4)
                   Print "Apparent Power=" ; Work(5)
                   Print "Active Power=" ; Work(6)
                   Print "Load=" ; Work(7) ; "%"
                   Print "Buss Volts=" ; Work(8)
                   Print "Battery Volts=" ; Work(9)
                   Print "Battery Current=" ; Work(10) ; "Amps"
                   Print "Battery Capacity=" ; Work(11) ; "%"
                   Print "Inverter Heat Sink Temp=" ; Work(12) ; "C"
                   Print "PV Current into Battery=" ; Work(13) ; "Amps"
                   Print "PV Voltage=" ; Work(14) ; "Volts"
                   Print "Solar Charger Battery Voltage=" ; Work(15) ; "Volts"
                   Print "Battery Discharge Current=" ; Work(16) ; "Amps"
                   A = Mid(work(17) , 1)
                      If A = "1" Then
                          Print "SBU Priority=Yes"
                               Else
                          Print "SBU Priority=No"
                      End If
                   A = Mid(work(17) , 2)
                      If A = "1" Then
                         Print "Configuration Change"
                          Else
                         Print "Configuration No Change"
                      End If
                    A = Mid(work(17) , 3)
                      If A = "1" Then
                         Print "Solar Charger Firmware Updated"
                          Else
                         Print "Solar Charger Firmware Not Updated"
                      End If
                     A = Mid(work(17) , 4)
                      If A = "1" Then
                        Print "Load is On"
                          Else
                        Print "Load is Off"
                      End If

                      A = Mid(work(17) , 6 , 3)
                           D = Binval(a)
                      Select Case D

                            Case 0

                            Print "No charging On"

                            Case 6

                            Print "Solar charging On"

                            Case 5

                            Print "AC charging On"

                            Case 7

                            Print "Solar & AC Charging On"

                      End Select
    End Select
      Call Cal_crc16


 End Sub


 'get the individual parameters returned
 Sub Chopup

 Local Lx As Byte

           Lx = Split(uart_command , Work(1) , " ")
           'Print "we had " ; Lx ; " lots of data returned"

 End Sub

 Sub Cal_crc16
 Local Z As Word
 Local Rcrc As Word



   Z = Len(uart_command)
   Z = Z - 2                                                'last two bytes are crc
   'Print "Z is>" ; Z
  Rcrc = Crc16(aruart_command() , Z)                        'calculate  the CRC
   Swap Rcrc                                                'bytes are in reverse order to what is sent to us
  Print "Cal CRC  is>" ; Hex(rcrc)
  Crc_16 = Right(uart_command , 2)                          'get the last two bytes the crc
  Rcrc = Work2                                              'this is a word variable overlayed on Crc_16

  Print "The sent crc>" ; Hex(rcrc)


 End Sub
 
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5009
Location: Holland

blank.gif
PostPosted: Mon Jan 25, 2016 10:26 am    Post subject: Reply with quote

thanks for sharing Paul. I do not know this MPP solar product but i am interested in solar products. A lot of panels, a windmill and a tesla battery for backup would be great Very Happy
_________________
Mark
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1192
Location: SYDNEY

australia.gif
PostPosted: Wed May 25, 2016 10:36 am    Post subject: Reply with quote

Hello Northland

Welcome to the Bascom forum Very Happy

You need to have the RS232 to TTL buffers to plug the Arduino into the PIP inverter as it is +-15volt
and the Arduino can only take 5volts
Have you got them yet?

Now with the mega2560 you need to load the Arduino driver for it so that
in device manager you can see it as a com port
once you have that you go to options > programmer and select ARDUINO STK500V2
Set the com port to that of the mega2560 and BAUD 115200
click on OK
Now bascom can talk to the arduino

You can test this by pressing F4 on your keyboard
You will now have the programming console up
Move mouse up to chip then identify
Bascom will now interrogate the arduino it should then display the ATMega2560 in the top left
This will have confirmed that bascom is talking to the arduino

Next post step two
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1192
Location: SYDNEY

australia.gif
PostPosted: Wed May 25, 2016 11:05 am    Post subject: Reply with quote

Now that you have pasted the code into bascom you need to save it to a file
So file save as (just as usual with windows)

Next you will press F7 this will compile the program into a binary file for the arduino to use

Next press F4 up comes the programming interface
Just above the word flash is a dark green icon when you move the mouse over it it will say Erase and Program Chip
by clicking on it bascom will put the program into the arduino

You now have the arduino working out the CRC for the PIP
COM port 2 on the arduino goes to the PIP using the RS232 buffer and TX RX cross over cable
COM port 1 is the USB cable to the arduino at 57600 BAUD

Now with a terminal program (I like realterm http://realterm.sourceforge.net/)
You can type QPIGS into the terminal press enter
The arduino works out the CRC then sends it to the PIP inverter
The PIP sends back the reply
The arduino receives this and works out what it is
It then prints all the data below to the terminal screen filling in the values (work() )

Thus the Arduino communicates with the inverter and translates the received data into meaningful values
Thus a program on a PC that sends QPIGS via the arduino COM port will get the data returned and decoded

I hope I have explained it clearly enough

When I make the server binary file you will be able to use bascom to load it into the arduino
even though it can not be compiled on the free version which has a limit of 4K

Regards Paul




Code:

                Print ">>Inverter Stats<<"
                   Print "Grid Volts=" ; Work(1)
                   Print "Grid Frequency=" ; Work(2)
                   Print "AC Output Volts=" ; Work(3)
                   Print "AC Output Frequency" ; Work(4)
                   Print "Apparent Power=" ; Work(5)
                   Print "Active Power=" ; Work(6)
                   Print "Load=" ; Work(7) ; "%"
                   Print "Buss Volts=" ; Work(8)
                   Print "Battery Volts=" ; Work(9)
                   Print "Battery Current=" ; Work(10) ; "Amps"
                   Print "Battery Capacity=" ; Work(11) ; "%"
                   Print "Inverter Heat Sink Temp=" ; Work(12) ; "C"
                   Print "PV Current into Battery=" ; Work(13) ; "Amps"
                   Print "PV Voltage=" ; Work(14) ; "Volts"
                   Print "Solar Charger Battery Voltage=" ; Work(15) ; "Volts"
                   Print "Battery Discharge Current=" ; Work(16) ; "Amps"

 
Back to top
View user's profile
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1192
Location: SYDNEY

australia.gif
PostPosted: Sat May 28, 2016 9:13 am    Post subject: Reply with quote

Try setting the programmer to ARDUINO instead of ARDUINO STK500V2 as your board uses a different usb to serial chip
I have the mega2560 boards so I can not test with mega1280

As you have a different chip on your board the regfile needs to be changed

I have some NANO boards on the way I will be looking at making them slaves off the main
board communicating back to to it.

Change

Code:


$regfile = "m2560def.dat" this needs to be changed

$regfile = "m1280def.dat"  to this



 
Back to top
View user's profile
Northland







PostPosted: Sat May 28, 2016 9:34 am    Post subject: Reply with quote

Yes I had 1280 there always. I tried every programmer in the list. Could it be the 2 files it says are missing? I googled both but nothing came up
Back to top
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1192
Location: SYDNEY

australia.gif
PostPosted: Sat May 28, 2016 9:53 am    Post subject: Reply with quote

No the two missing files are the program for the Arduino

The BIN file is what gets loaded into the flash
The EEP file is what gets loaded into the the eeprom

So you have tried the different options but should only need the Arduino setting
sometimes I find you have to save the setting then close bascom
then re start bascom

Regards Paul
Back to top
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 923
Location: France

france.gif
PostPosted: Sat May 28, 2016 5:59 pm    Post subject: Reply with quote

hi Northland,

did you work with an Arduino 2560 yes or not, you work with an AVR M1280 Yes ?
what kind of arduino module did you use ?

I use M2560 or Chinese compatible, but for the Chinese compatible I try to use with IDE Arduino first to be sure it was recognised then I look for the serial port of the module.
maybe you can use a baud rate of 57KB ?
let us know, please.

If you try to program a M1280 alone you need a programmer as USB-ISP or others...look at the list and the help file in the help.
JP
Wink
Back to top
View user's profile Visit poster's website
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 923
Location: France

france.gif
PostPosted: Sun May 29, 2016 9:42 am    Post subject: Reply with quote

look at here first :
http://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=12472
try the "blink" with IDE arduino
change the program al little bit and try it again.
check the baud rate and the port
if it is ok
look at if the test-led is really on the pin B.7 (pin13 arduino)

start bascom

modify the $regfile in the program below and adjust the baud rate of the programmer following the baud rate of IDE arduino

'-------------------------------------------------------------------------------
' ARDUINO-M2560 V3.BAS
'
' (c) 1995-2014, MCS Electronics
' This is a sample file for the Mega2560 based ARDUINO board
' Select Programmer 'ARDUINO stk500V2' , 115200 baud and the proper COM port timeout 200
'-------------------------------------------------------------------------------

$regfile = "m2560def.dat"

$crystal = 16000000
'Config Clockdiv = 1 ' either use this or change the divider fuse byte

Config Portb.7 = Output 'pin 13 arduino


Do
Wait 2
Toggle Portb.7

Loop
compile, choice the good programmer autosave , without autoverfy
save and exit from bascom

reload bascom

press F4 when the program is loaded and compiled (F7) and wait without any action
If you have pb again use DUDE
http://wiki.mcselec.com/Getting_started_with_Arduino_UNO take care it is .hex file not .bin one

JP
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 676

poland.gif
PostPosted: Sun May 29, 2016 9:49 am    Post subject: Reply with quote

I also have Arduino IDE installed so I look inside bootloaders section.
In bootloaders for Atmega you can find bootloader for 1280 and some usefull informations
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 676

poland.gif
PostPosted: Sun May 29, 2016 9:54 am    Post subject: Reply with quote

So I think you should try like that. But after set this parameters restart Bascom.
I dont know why but sometimes when I switch from one programmer to another (MKII for Xmagas and switch to Arduino for example) then without restart programmer fail. But after restart everything goes OK so I restart bascom "to be sure" Very Happy

You can also visit http://wiki.mcselec.com/bavr/ARDUINO
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5009
Location: Holland

blank.gif
PostPosted: Sun May 29, 2016 10:25 am    Post subject: Reply with quote

Northland wrote:
Yes I had 1280 there always. I tried every programmer in the list. Could it be the 2 files it says are missing? I googled both but nothing came up


sure, you first should check why these files are mssing.
at least the BIN file should exist. without the binary to program, no programming is possible.

and you should also let us know which bascom version you use.

_________________
Mark
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1192
Location: SYDNEY

australia.gif
PostPosted: Mon May 30, 2016 11:06 am    Post subject: Reply with quote

Ok my nano boards arrived today and with ARDUINO set as programmer and BAUD 57600
it is working with bascom chip identify
Now to see if I can get the program working
using bascom's software UART to create the second serial port

Regards Paul
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
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